Get the Content of a Directory in Java Friday, Jun 12 2009 

First you create a class that implements java.io.FilenameFilter and then code the accept() method, then call File.list() with the filter as a parameter. The returned array of strings has all the names that passed through the accept()filter.

import java.io.File;
import java.io.FilenameFilter;

public class Filter implements FilenameFilter {
  protected String pattern;
  public Filter (String str) {
      pattern = str;
  }

  public boolean accept (File dir, String name) {
      return name.toLowerCase().endsWith(pattern.toLowerCase());
  }

  public static void main (String args[]) {
      if (args.length != 1) {
         System.err.println
          ("usage: java Filter   ex. java Filter java");
         return;
    }

    Filter nf = new Filter (args[0]);
    // current directory
    File dir = new File (".");
    String[] strs = dir.list(nf);
    for (int i = 0; i < strs.length; i++) {
        System.out.println (strs[i]);
    }
  }
}

(more…)

Optimization Techniques when Concatenating Strings Saturday, May 30 2009 

You can concatenate multiple strings using either + operator or String.concat() or StringBuffer.append(). Which is the best one interms of performance?

The choice depends on two scenarios,first scenario is compile time resolution versus run time resolution and second scenario is wether you are using StringBuffer or String. In general, programmers think that StringBuffer.append() is better than + operator or String.concat() method. But this assumption is not true under certain conditions.


1) First scenario: compile time resolution versus run time resolution

Look at the following code StringTest3.java and the output.
(more…)

Optimization by Initializing StringBuffer Saturday, May 30 2009 

You can set the initial capacity of StringBuffer using its constructor this improves performance significantly. The constructor is StringBuffer(int length), length shows the number of characters the StringBuffer can hold.

You can even set the capacity using ensureCapacity(int minimumcapacity) after creation of StringBuffer object. Initially we will look at the default behavior and then the better approach later.

The default behavior of StringBuffer:

StringBuffer maintains a character array internally.When you create StringBuffer with default constructor StringBuffer() without setting initial length, then the StringBuffer is initialized with 16 characters. The default capacity is 16 characters. When the StringBuffer reaches its maximum capacity, it will increase its size by twice the size plus 2 ( 2*old size +2).

If you use default size, initially and go on adding characters, then it increases its size by 34(2*16 +2) after it adds 16th character and it increases its size by 70(2*34+2) after it adds 34th character. Whenever it reaches its maximum capacity it has to create a new character array and recopy old and new characters. It is obviously expensive. So it is always good to initialize with proper size that gives very good performance.

Its really effective way for initialization of StringBuffer. So it is always best to initialize the StringBuffer with proper size.
Source: PreciseJava

Optimization of Stings in Java Saturday, May 30 2009 

In situations where String objects are duplicated unnecessarily, String.intern() method avoids duplicating String objects. The following figure shows how the String.intern() method works. The String.intern() method checks the object existence and if the object exists already, it changes point of reference to the original object rather than create a new object.

The following figure shows the creation of String literal and String Object using intern

image002
Here is the sample code to know the importance of String.intern() method. (more…)

How the JVM Works with Strings Saturday, May 30 2009 

Java Virtual Machine maintains an internal list of references for interned Strings ( pool of unique Strings) to avoid duplicate String objects in heap memory. Whenever the JVM loads String literal from class file and executes, it checks whether that String exists in the internal list or not. If it already exists in the list, then it  does not create a new String and it uses reference to the existing String Object. JVM does this type of checking internally for String literal but not for String object which it creates through ‘new’ keyword. You can explicitly force JVM to do this type of checking for String objects which are created through ‘new’ keyword using String.intern() method. This forces JVM to check the internal list and use the existing String object if it is already present. (more…)

Better way of creating Strings Saturday, May 30 2009 

You can create String objects in one of the way.

1.String s1 = "hello";
  String s2 = "hello";
2.String s3 = new String("hello");
  String s4 = new String("hello");

Which of the above gives better performance?
Here is a code snippet to measure the difference.

(more…)

GWT installation and creating a GWT Project Friday, May 22 2009 

Installing GWT


To develop programs using the GWT we need to first download it from here : GWT download. Once you have it installed the next step would be to update the PATH environmental variable. For example if you have installed GWT in you C: drive as C:\gwt-windows-1.5.3 then append this path to the end of the PATH variable. Once this is done you are ready to create GWT projects.
[Note: You need JDK 1.5 or above to develop GWT projects. You also need an IDE to develop the GWT projects. We will use Eclipse IDE in this tutorial]
(more…)

Accessing the system tray: SysTray for Java Thursday, May 7 2009 

SysTray for Java enables Java applications to create an icon in the system tray under Windows and KDE 3.

SysTray for Java is an API written to enable Java applications access the system tray. Originally it was running on Windows only. In the meantime it is also available for KDE3It supports the creation of submenus, multiple independent menus, checkable menus, and everything can be changed at runtime.
Licence: The JAR and the DLL needed to run systray4j on Windows are LGPL. The code for KDE is GPL.

Google Web Toolkit Basics Tuesday, Apr 14 2009 

What is and Why GWT?

What is GWT?

• Java software development framework that makes writing AJAX applications easy
• Let you develop and debug AJAX applications in the Java language using the Java development tools of your choice.

> NetBeans or Eclipse

• Provides Java-to-JavaScript compiler and a special web browser that helps you debug your GWT applications

>When you deploy your application to production, the compiler translates your Java application to browser-compliant JavaScript and HTML.

You can think of GWT as Java software development framework for building AJAXapplication. In other words, it lets you develop and debug AJAX applications in he Java development environment. You can use your favorite development tool suchas NetBeans or Eclipse to develop the application. When you are done with thetesting and debugging, you can deploy the application. During the deploymentphase, the GWT compiler compiles the Java code into JavaScript and HTML. (more…)

Check boxes and Radio buttons With JQuery Tuesday, Mar 24 2009 

Using JQuery is little but different here because you have to use little more that just ID selectors:

Check box and radio buttons are two controls which are normally used for state data like option is True/False and Is selected from some of options given.

Usage of JQuery is comparatively simple for input type text, button, file, reset etc. because these elements are used to capture the data instead of any state holding capacity. (more…)

Next Page »