View RSS Feed

Java SE

Java Standard Edition

  1. Implementing ActionListener interface

    by , 11-12-2011 at 05:54 PM (My Java Tips)
    ActionListener interface is used to perform actions on a performed event. In this post, I will write about its importance and usage.


    ActionListener interface is part of java.awt.event package. It has only one abstract method that has to be implemented in the class implementing this interface.

    Method signature:
    void actionPerformed(ActionEvent e)

    Consider the following scenario: you developed an GUI form for an application comprising of text fields, ...
  2. Creating ZIP files

    by , 11-12-2011 at 05:49 PM (My Java Tips)
    We all use ZIP files in daily routine. They are of great use specially when we want to transfer the files over the network. In this post, I will write about how to create zip files in Java.


    Java.util.zip package provides classes to play with zip files. So first step is to import the package. We will need ZipOutputStream and ZipEntry class from this package. Import the complete package or just these classes - it depends on you. It does not have any affect on the performance.
    ...
    Categories
    Java SE
  3. Incorporating native code (C/C++)

    by , 11-12-2011 at 05:42 PM (My Java Tips)
    JNI (Java Native Interface) is a powerful feature that provides the developers to call legacy code written in C/C++ and Java. In the post, I will briefly introduce how JNI helps us in doing so.


    First step is to create a Java class that declares native method. Native method’s signature contains keyword native and it indicates that this method is implemented in another language. Native method declaration is terminated with a semicolon. Also note that native method does not contain implementation. ...
    Categories
    Java SE
  4. Internationalization

    by , 11-12-2011 at 05:31 PM (My Java Tips)
    Internationalizing Java applications is of great importance if you are planning to develop an application to be used in different parts of the world. In this post, I will try to develop a simple internationalized application.


    Different regions of the world have different languages, currency and time zone. Developing a Java application that can be used throughout the world without much changes is the requirement of this age. Internationalized applications are the solution. Internationalized ...
  5. Creating JAR files

    by , 11-12-2011 at 05:25 PM (My Java Tips)
    JAR (Java Achieve Files) are compressed files that are used to bundle multiple files into a achieve file so the distribution can be managed easily. In this post, I will write about how to create JAR files.


    JDK includes a JAR tool that is used for creating JAR files. Command to use is:
    jar cf jar-file input-file(s)

    c indicates that we want to create a JAR file.
    f indicates that ouput should go to the file
    jar-file is the name of the jar file that ...
    Categories
    Java SE
  6. System class

    by , 11-10-2011 at 06:23 PM (My Java Tips)
    The System class is a very useful which contains static fields and methods. It cannot be instantiated. In this post, I will briefly write about System class and will explore few interesting methods.


    System class belongs to java.lang package, so it can be used in any Java class without importing any package. System class provides static methods that are very useful. For example to get current time in milliseconds:

    Java Code:
    static long currentTimeMillis()
    ...
  7. Eclipse Templates

    by , 11-10-2011 at 05:30 PM (My Java Tips)
    Eclipse Templates are used to make programming easier and simpler. Eclipse IDE provides many templates by default which can be used to save time. Also one can add more templates as required. In this post, I will briefly write about creating and using templates in Eclipse.


    Open the preferences window in Eclipse. Under Java > Editor you will find Templates. Click it and you will see the available templates in the right had side window.Name:  templates.PNG
Views: 306
Size:  21.6 KB
    Making own ...
    Categories
    Java SE
  8. Matching Patters using Regex

    by , 11-10-2011 at 05:18 PM (My Java Tips)
    Java developers who are working on text parser or text extractor in Java face problems while search a specific pattern in the text. Searching patterns is not an easy task using standard java string operations. Fortunately we can use regular expressions in java which makes pattern matching easier and simply. Java.util.regex is the package which comes to our rescue.


    One should have a little knowledge of regular expressions to make use of regex. For instance [] is used to give range ...
    Categories
    Java SE
  9. Reading an ASCII file (BufferedReader)

    by , 11-10-2011 at 05:14 PM (My Java Tips)
    Normally we give input to Java applications from command prompt or through GUI. But sometimes, when inputs are in bulk, we can use ASCII files for input. Such files must have some agreed upon pattern so Java parser can understand the meaning of the text. There are many ways to read a file but one of the simplest way is to use BufferedReader class which is found in java.io package.


    First prompt the user to enter the file name with path. This can be done using:

    Java Code:
      
    ...
    Categories
    Java SE
  10. Copying Arrays

    by , 11-10-2011 at 05:10 PM (My Java Tips)
    Arrays are very useful and are commonly used in our Java programs. Array name is actually a pointer that points to the continuous memory location. Many Java developers make some mistakes while copying arrays, which become difficult to cater.


    Consider the following scenario:

    I have a String array called 'array' which have 5 elements in it. I declared another String array called 'array_tmp'. I want to copy the contents of 'array' to 'array_tmp'. One simple way is to do ...
    Tags: arrays, copying Add / Edit Tags
    Categories
    Java SE
  11. Using Vectors in Java

    by , 11-10-2011 at 05:03 PM (My Java Tips)
    Vectors are very useful to store objects for later use. One may define vector as a growable array of objects, so no need to specify the size of Vector while declaring it.


    Vector stores objects which can even be an object of user made class. For example: we want to store 2 string objects in a vector. Then we want to store an object of user made class TestClass also in the same vector. This is possible.

    Vectors belong to java.util package so import it before using. While ...
    Categories
    Java SE
  12. Saving objects to file

    by , 11-10-2011 at 04:39 PM (My Java Tips)
    Sometimes it is useful to store objects in files which can be read later. Object is saved along with its state. Simply saying, an object with its data can be saved in a file. Objects are made persistent this way.


    The class whose object is to be saved should implements Serializable interface. We as a programmer are not supposed to add any method. The purpose of implementing interface is to tell the Java run time, which classes are to be serialized.

    Serializable interface ...
  13. Retrieving serialized objects from file

    by , 11-10-2011 at 04:35 PM (My Java Tips)
    I assume that you are aware of how to serialize objects and save them into files on the disk. Now I will talk about how to retrieve serialized objects.


    You have to use FileInputStream and ObjectInputStream to fetch the object. Specify the file which contains the object in FileInputStream. Once ObjectInputStrem is initialized, just use in.readObject method of ObjectInputStream to fetch the object.

    In the following example, I am fetching a Vector from a file on disk. ...
  14. Using Interfaces

    by , 11-10-2011 at 04:32 PM (My Java Tips)
    Interfaces are used to define a contract for the classes. It has its own importance when it comes to defining standards.


    Consider the following example:

    Cars have some standard and their basic operations are the same. Each car should have operations like startCar, moveCar, stopCar etc. Toyota, Mazda and Honda are cars and each has its own way of starting, moving and stoping. So each has to have its own startCar, moveCar and stopCar method.

    How to enforce ...
  15. Serializing Arrays

    by , 11-10-2011 at 04:08 PM (My Java Tips)
    We already know that we can serialize object of classes that implement serializable interface. Make a class that implements serializable interface, make an object of it and save it to disk. You may retrieve to confirm that the object has the required data or not. Vector class also implements serializable interface, so you can also store vectors on disk.


    How about Arrays?

    Arrays are of some type. Thing to note is which classes implement Serializable interface. Integer, ...
    Categories
    Java SE
  16. Parsing XML Documents-1 (DOM and SAX)

    by , 11-10-2011 at 04:03 PM (My Java Tips)
    XML files are very useful. Usually they are used to store configuration settings. We can also use XML files to store data so it can be used as a database. A lot of work has been done in this domain and its really very useful working with XML files.



    Now suppose you have a XML file with some useful data.

    college.xml
    XML Code:
    <college>
    <student>
      <first>Shoaib</first>
      <last>Malik</last>
    ...
    Tags: dom parser, sax, xml Add / Edit Tags
    Categories
    Java SE
  17. Programming in an efficient way

    by , 11-09-2011 at 06:12 PM (My Java Tips)
    I have seen even matured programmers making few mistakes while programming, which might have performance bottleneck. I would like to mention few of such mistakes here.


    A function call takes some resources. Even it seems a simple line of code, Java Runtime has to take case of few things to manage it. Consider following example:

    Java Code:
    String str = "Lets play football. If you are interested, let me know.";
    for (int i=0; i < str.length();i++)
    ...
    Categories
    Java SE
  18. Defining own Exceptions

    by , 11-09-2011 at 06:10 PM (My Java Tips)
    Exception is defined as:
    "An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions."

    This event (exception generation event) can be specific to your requirement as well. Defining own exception makes sense if you want to generate exceptions specific to your logic (business logic). .


    Consider following scenario:

    You have an environment where student id cannot be ...
    Categories
    Java SE
  19. Varargs – Java 5.0 addition

    by , 11-09-2011 at 06:08 PM (My Java Tips)
    We all are familiar with methods in lava. Methods are an important artifact of programming. Method contains signature and body. Method body is enclosed in braces. Method signature comprises of its name, access modifier, return type and list of parameters.



    Java Code:
    public String methodName(String arg0,int arg1)
    {
    …
    return string;
    }
    Methods can also be static which means they can be accessed without using any object.
    ...
    Categories
    Java SE
  20. Using Reflection to run methods

    by , 11-09-2011 at 05:59 PM (My Java Tips)
    Reflection is used when we want examine or modify the runtime behavior of applications running in the Java virtual machine. Java’s Reflection API provides very useful functionality for programmers. Here we will go through an example to understand what can be done with Reflection.

    Consider the following class called Countries:


    Java Code:
    public class Countries {
    
    String name;
    String capital;
    
    public void setName(String name) {
    ...
    Categories
    Java SE
  21. using Packages

    by , 11-09-2011 at 05:57 PM (My Java Tips)
    Packages are used to group related classes, interfaces and enumerations together. Each package has its own namespace and provide access protection and name space management.


    Using packages in your projects is a good programming approach. It makes more sense to use packages if you are working in a team on some big project.

    Suppose you write a group of classes that represent vehicle objects like bus, truck and car. You also have interfaces called Drive and Stop and each ...
    Categories
    Java SE
  22. Using enum types (with example)

    by , 11-09-2011 at 05:55 PM (My Java Tips)
    Sometimes it is useful to have a set of values for a variable. Examples can be days of week, planets in the solar system, months of a year, grades of a course, directions etc. This control is useful specially when you are developing a software in a team. So you define boundaries of that type and others have to follow those rules.


    In such scenarios, we use enum types. An enum type implicitly extend java.lang.Enum. Its declaration syntax is easy:

    Java Code:
    public enum Day
    ...
    Categories
    Java SE
  23. Inheritance by example

    by , 11-09-2011 at 05:53 PM (My Java Tips)
    I have fond many developers who very seldom use inheritance while coding in Java. The reason they give is why one should make code difficult to understand? I am amazed at this reason because Java inheritance is similar to natural inheritance.


    Lets fist understand the basics.

    Mazda is a Car > Mazda inherits Car
    Java is a programming language > Java inherits Prog. Language
    Australia is a country > Australia inherits Country

    Whenever ...
    Categories
    Java SE
  24. Method shadowing (Inheritance)

    by , 11-09-2011 at 05:50 PM (My Java Tips)
    Method shadowing is a term used for situations, when child and parent classes have methods with exactly same signature. This situation can create problems that sometimes become difficult to understand.



    Method shadowing can be done on purpose or by mistake.

    Consider the example below:

    Java Code:
    public class Car {
    
         	public void startCar()
    	{
    		System.out.println("Car class - Car started");
    	}
    ...
    Categories
    Java SE
  25. Introduction to Hashtable

    by , 11-09-2011 at 05:46 PM (My Java Tips)
    Class Hashtable is found in java.util package and is very useful data structure. If used sensibly, it can save time and can produce results efficiently. Hashtable class implements Cloneable, Map and Serializable interfaces.


    Hashtable can be simply a table in the memory. Each value stored as a key associated to it. Values are retrieved also using keys so searching is very fast.. It is always a good idea to declare Hashtable of size much greater than what you need for faster processing/lookups. ...
    Categories
    Java SE
  26. Method parameters bounded by a Class

    by , 11-09-2011 at 05:45 PM (My Java Tips)
    Java Generics are introduced in Java 5.0 and have introduced new exciting ways to code. In this post, I will show you how you can use generic types for method parameters.


    Lets take an example: You have Integer, Float, BigInteger and Double values and want to compare them using user made functions.
    You have to write separate functions for comparing Integer with BigInteger, Integer with Double, Integer with Float and so on. But there is an easy way to it. Simply use generics. ...
    Categories
    Java SE
  27. Javadoc tags

    by , 11-09-2011 at 05:40 PM (My Java Tips)
    JavaDoc comments start with a forward slash and two asterisks (/**) and ends with one asterisk and a forward slash (*/). Everything between the delimiters (even if it spans multiple lines) is a comment.



    /**
    * @author Gapper
    * @version 1.0
    */

    One has to use javadoc tags. The following are special tags in Javadoc that have predefined purposes.

    @see
    It is used to create links to other javadoc documentation.
    ...
    Categories
    Java SE
  28. Abstract Classes/Methods

    by , 11-09-2011 at 04:58 PM (My Java Tips)
    The keyword abstract is used when a class is declared abstract. An abstract may or may not include abstract methods. These classes can be inherited (subclasses) but cannot be instantiated.


    Abstract classes comprise of concrete methods and abstract methods. Concrete methods are methods with body and abstract methods are without body methods. The class implementing the abstract class has to override abstract methods.

    Abstract methods are without implementation (without ...
    Categories
    Java SE
  29. An Introduction To JUnit

    by , 11-08-2011 at 06:51 PM (My Java Tips)
    In a big project programmer needs to test his code as a unit, Separately from other part of project. So there was a need of some framework which can provide testing for implementing testing in Java.

    JUnit provides a simple way to explicitly test specific areas of a Java program. It can be used to test a hierarchy of program code either as individual or as multiple units. Using JUnit framework is beneficial because it forces us to explicitly declare the expected results of specific ...
    Tags: junit Add / Edit Tags
    Categories
    Java SE
  30. Execute An External Program From Java

    by , 11-08-2011 at 06:49 PM (My Java Tips)
    A Java programmer may need to access the environment under which the application is running. It is supported by “ getRuntime ” method in Runtime Class. Programmer need not create an instance of Runtime class, Instead the Java application has a single instance of class Runtime environment in which the application is running.

    Runtime class has several methods that are useful to execute an external program, getting Information about processors, Memory Information etc.

    ...
    Categories
    Java SE
Page 2 of 5 FirstFirst 1234 ... LastLast