View RSS Feed

Java SE

Java Standard Edition

  1. Java Compiler API (brief intro)

    by , 11-28-2011 at 06:49 PM (My Java Tips)
    We all know that javac command is used to compile the java classes. Even if we are using some IDE (Eclipse, JBuilder, NetBeans), javac is called at the background for compilation. But with the release of Java 6, it has become possible to compile Java class from a Java class.

    The classes related to Java compiler are packed into package.

    There is a class called ToolProvider ( whose getSystemJavaCompiler() method returns an instance of some class ...
    Tags: ide, javac Add / Edit Tags
    Java SE
  2. Compiling a Java class using JavaCompiler

    by , 11-28-2011 at 06:47 PM (My Java Tips)
    Java 6 introduces a way to compile Java classes from a Java class. In this post, I will present an example to show how this is done.

    I have a file:

    Java Code:
    public class Test{
    public static void main(String[] args) {
            // TODO code application logic here
            System.out.println("Hello World");
        public static void callMe(){
         System.out.println("Hello Babar dost");
    Java SE
  3. Accessing Resources in a JAR File

    by , 11-28-2011 at 06:05 PM (My Java Tips)
    JAR files are used to deploy applications. They comprise of java classes and other resources like images etc. In this post, I will explain how to access resources packed in a JAR file.

    To access resources in a JAR file, we use getResource method. Lets do this practically. The code snippet provided shows how to retrieve images from a JAR file.

    Java Code:
    // Get current classloader
    ClassLoader cl = this.getClass().getClassLoader();
    // Create icons
    Java SE
  4. Creating JAR files

    by , 11-28-2011 at 06:02 PM (My Java Tips)
    This post is all about learning how to create JAR files. After going through this, you will be able to easily create JAR files.

    The basic JAR command syntax is as follows:

    jar cf myjar input-file(s)

    Let me explain the command;

    - c option indicates that I want to create a JAR file
    - f option indicates that I want the output to go to a file rather than to stdout
    - myjar is the name that I want the JAR file to have. It can be ...
    Java SE
  5. Package Explorer - Class Method

    by , 11-21-2011 at 05:01 PM (My Java Tips)
    In this post, I will write about Eclipse feature that shows the artifacts of a class method.

    Eclipse package explorer is a very useful explorer that gives the developer a birds eye view of the package, class and methods. Review the class below:

    Java Code:
    public class Student {
    	private int id;
    	private String name;
    	private static int counter;
    	private static final int MAXCOUNT=10;
    	public Student(int id, String name)
  6. Object references

    by , 11-21-2011 at 04:58 PM (My Java Tips)
    In this post, I will be talking about how to refer to an object. I will present a simple example to make things obvious.

    We have a class called Student. It had 2 attributes, getter setter methods and a method named showAll() to show the contents of both the attributes.

    Java Code:
    public class Student {
    	private int id;
    	private String name;
    	public Student(int id, String name) { = id; = name;
  7. Method signatures

    by , 11-21-2011 at 04:52 PM (My Java Tips)
    In this post, I will present an interesting example related to method signature.

    Method signature comprise of the following:

    - method name
    - return type
    - input parameters
    - access modifier

    For example:

    Java Code:
    public Vector processText(String test, int id)
    Two methods in a class can have same name provided then have different parameters. Different parameters means different no of parameters and ...
  8. Using static and final attributes – An example

    by , 11-21-2011 at 04:47 PM (My Java Tips)
    In this post, I will present an example which used static and final keywords with attributes in meaningful way. I hope after going through this, you will develop good understanding of these.

    As may know, that we may access static member of class without using any object of that class. Final field/attribute means that you cannot change its value. If you try to change the value of final attribute, you will get an error – some this like this:

    Exception in thread ...
  9. An interface extending an interface

    by , 11-20-2011 at 05:37 PM (My Java Tips)
    The subject topic is very interesting and I decided to write something on it. I hope it will be useful.

    You know about the interfaces. They do not contain the implementation details, rather they contain the method signatures. The class implementing an interface, has to provide the implementation for each interface method. An interface in implemented using the keyword implements.

    Java Code:
    Class Room implements IOffice
    The keyword extends ...
  10. Enhanced for-loop (Java 5.0)

    by , 11-20-2011 at 05:14 PM (My Java Tips)
    Java 5.0 introduced an enhanced for-loop. In this post, I'll write about it.

    The enhanced for loop provides a simple structure allows one to simplify code by presenting for-loops that visit each element of an array/collection without explicitly expressing how one goes from element to element.

    The new for loop makes programming simpler. For example, if you want to print each element for an array, you used to do that with a for loop in the following way:

    Java SE
  11. Handling ASCII character set in Java

    by , 11-19-2011 at 06:11 PM (My Java Tips)
    In this post, I will talk about ASCII character set and how to deal with ASCII codes in Java.

    ASCII character uses 7 bits for character representation. This means that there are total of 128 characters supported by ASCII set. Point to note is that there are only 93 printable characters in ASCII whose ASCII codes are from 33 till 126.

    Java supports all popular character including UTF. But sometimes you only want the input to comprise of ASCII printable characters. This ...
  12. Applets with parameters

    by , 11-19-2011 at 05:37 PM (My Java Tips)
    Applets may accept parameters. This post briefly shows how to do that.

    Applet in included in HTML using APPLET tag. You have to specify the parameters in APPLET tag in the following form:
    Java Code:
    <applet code="AppletSubclass.class" width="anInt" height="anInt">
    <param name="parameter1Name" value="aValue"></param>
    <param name="parameter2Name" value="anotherValue"></param>
    Java SE
  13. Validating ASCII character set

    by , 11-17-2011 at 07:04 PM (My Java Tips)
    In this post, I will write about how to validate characters of ASCII characterset.

    ASCII is a well know character set. It is a 7 bit character set and represents 128 characters. The printable character codes range from 33 to 126 inclusive. Sometimes you want to make sure that your strings do not contains non ASCII printable character. This is easy and can be done by writing a method for this.

    Review the static method below. It takes an ArrayList of String type and after ...
  14. Java Generics with legacy code

    by , 11-15-2011 at 06:31 PM (My Java Tips)
    In this post, I will write about how Java generics help us in using legacy code.

    Generics are new in Java and now developers understand the importance of it and it is being used as required. Question arises, what about the legacy code? Consider an IT firm that is working on an application for 5 years, and they used Java 1.4 for development. They the plan to use 1.5 for development simply because it is better than 1.4. Are they supposed to convert the old code to 1.5 also? This is ...
  15. Capture screenshots

    by , 11-14-2011 at 05:41 PM (My Java Tips)
    Taking screenshots in windows is simple and very useful in our day to day business. It is done by pressing the Print Screen key on your keyboard [PrtScn], pasting it in image editor such as MS paint and then editing and saving it. In this post, I will write about how to do this from Java program.

    Java provides classes to help us which capturing screens.

    java.awt.Robot is used to generate native system input events which can be used to for test automation, and other ...
  16. Bounding ArrayList

    by , 11-13-2011 at 11:45 AM (My Java Tips)
    Collections can be of a particular type i.e they are only allowed to hold objects of a defined type. This is called “bounded by”. For example:

    Java Code:
    ArrayList  arrayList  = new  ArrayList();
    Vector  vector  = new  Vector();

    We declared an ArrayList and a Vector both bounded by String. We cannot store objects other than String in these. Lets try to store an integer in Vector and see what happens.

    Java Code:
    Vector  vector  = new  Vector();
  17. Interface extending Interface

    by , 11-13-2011 at 11:42 AM (My Java Tips)
    An interface can extend other interface but cannot implement any interface. This makes sense because interface cannot have any implementation. An interface can only contain abstract methods that are implemented by the class implementing that interface.

    Lets do this with an example.

    Create a package named myinterfaces. Using conventions, package name should be in small case. Now create an interface named InterfaceA with 2 methods.

    Java Code:
    package myinterfaces;
  18. Using LinkedList (Collection)

    by , 11-13-2011 at 11:40 AM (My Java Tips)
    LinkedList is a special type of List which is very useful in certain scenarios. Some important facts about LinkedList are:
    - It is not synchronized
    - It does not use array internally for storage
    - It uses pointers internally to point to next node
    - It implements Cloneable, List and Serializable interfaces

    LinkedList is preferred when you know that you have to add and delete data from the start or from middle of the list. It perform addition and deletion ...
    Java SE
  19. Implementing more than one Interfaces

    by , 11-13-2011 at 11:37 AM (My Java Tips)
    Interfaces contain abstract methods that implementing class has to implement. It is must for the implementing class to implement all the methods found in the interface. Sometimes you face a situation where you have to implement more than one interface. How to do that?

    One way is to simply try to write one interface with all the abstract methods. This is not at all flexible as all of these interfaces might be used in different scenarios. So its better to keep each as a separate interface. ...
  20. Constructor calling ordering

    by , 11-13-2011 at 11:34 AM (My Java Tips)
    Constructors are used for initialization normally. Name of constructor should be same as that of the class. If no constructor is declared, a default constructor is created without parameters. Constructor does not return any thing.

    Consider the example below. ClassB inherits from ClassA. In the MainClass, we made an object of ClassB. Default constructor will be called. But the output suggests that first Constructor of ClassA is called and then the constructor of classB is called. ...
  21. Using Treeset

    by , 11-13-2011 at 11:29 AM (My Java Tips)
    TreeSet belongs to Java Collection Framework and is very rare situations. In this post, I will briefly discuss when to use TreeSet and how to use it.

    To use it, you have to import java.util.TreeSet in your class. TreeSet is not synchronized so if a TreeSet is concurrently accessed by threads then thread modifying the contents must be synchronized externally. TreeSet provides following constructors:
    TreeSet(Collection c)
    TreeSet(Comparator c)
    TreeSet(SortedSet ...
  22. Persisting state of objects

    by , 11-13-2011 at 11:24 AM (My Java Tips)
    Serializable interface has no methods and fields so a class that implementing this interface does not have to override any method. The state of objects of serializable class, can be saved and restored later. The state is saved on a container that may be transient (RAM-based) or persistent (disk-based).

    For network communication, the objects that you want to transfer through streams, should be Serializable. For instance, Vector, ArrayList etc implement Serializable interface so their ...
  23. Randomly Accessing Files

    by , 11-13-2011 at 11:12 AM (My Java Tips)
    I have seen a lot of Java guys struggling with accessing a file and playing the data in it. File input output operations are obviously slow as compared to "in memory" operations but then you have persistent data which is of good use. Someone can argue that databases should be used for input and output but then it’s a separate debate. Just to keep it short, I would say that sometimes it is better to use text files for input and out.

    In this post, I will write about randomly ...
  24. Multithreading (basics)

    by , 11-13-2011 at 10:59 AM (My Java Tips)
    Multithreading is a concept where a program is broken into two or more parts called threads and all these threads run in parallel. Multithreading can make programs more responsive and effective and it increases its performance too. For example, today web pages need to display animations with sound effects and text at the same time. If this is done by using the traditional single-threaded event loop, the application will take a lot of time to get loaded, and time is a very crucial factor for web ...
    Java SE
  25. Why use multithreading?

    by , 11-13-2011 at 10:55 AM (My Java Tips)
    In this post, I will talk about the importance of multithreading and would like to answer the question: Why use multithreading ??

    Multithreading exploits the fact that most of the time the tasks (parts) of the same program are either waiting for the other resources to become free, or waiting for some timeout to occur. In the above example (spreadsheet), scroll operation is waiting for the calculation to be completed. If these parts or tasks can be described as independent threads, ...
    Java SE
  26. Java Threads

    by , 11-13-2011 at 10:51 AM (My Java Tips)
    A thread is a thread of execution in a program. Java environment has been built around the multithreading model. In fact all Java class libraries have been designed keeping multithreading in mind. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. If a thread goes off to sleep for some time, the rest of the program does not get affected by this. Similarly, an animation loop can be fired that will not stop the working of rest of the system. ...
  27. Thread priorities, synchronization and messaging

    by , 11-13-2011 at 10:47 AM (My Java Tips)
    I assume that you have the basic knowledge of threads. In this post, I will write about thread priorities, synchronization and messaging.

    In multithreading environment, one thread might require the attention of the CPU more quickly than other. In such a case that thread is said to be of high priority. Priority of a thread determines the switching from one thread to another. In other words, priority determines how a thread should behave with respect to the other threads.
  28. main() thread in Java

    by , 11-13-2011 at 10:44 AM (My Java Tips)
    The 'main()' method in Java is referred to the thread that is running, whenever a Java program runs. It calls the main thread because it is the first thread that starts running when a program begins. Other threads can be spawned from this main thread. The main thread must be the last thread in the program to end. When the main thread stops, the program stops running.

    Main thread is created automatically, but it can be controlled by the program by using a Thread object. The Thread ...
    Java SE
  29. Creating a Thread (extending Java Thread Class)

    by , 11-13-2011 at 10:41 AM (My Java Tips)
    There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.

    In case, ...
  30. Creating a Thread (implementing Java Runnable Interface)

    by , 11-12-2011 at 05:58 PM (My Java Tips)
    A better way to create a thread in Java is to implement Runnable interface. A thread can be created by extending Java Thread class also. Now the question arises why implementing Runnable interface is a better approach? Answer is, if the thread class you are creating is to be subclass of some other class, it can’t extend from the Thread class. This is because Java does not allow a class to inherit from more than one class. In such a case one can use Runnable interface to implement threads.
Page 1 of 5 123 ... LastLast