Thursday, April 15, 2010

JDK 5 Enhancements in Java Language


1.      Generics - This is an enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety. It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting.
e.g.
Previously
Array List numbers = new ArrayList();

Now in JDK 5.0
Array List<Integer> numbers = new ArrayList<Integer> ();

2.      Enhanced for Loop - This new language construct eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays.
e.g.      
Previously
Iterator itr = numbers.iterator();
while
 (itr.hasNext()) 
{
      Integer element = (Integer)itr.next();
      System.out.println (number);
}

Now in JDK 5.0
for (Integer number: numbers)
{
            System.out.println (number);
}

3.      Autoboxing/Unboxing - This facility eliminates the drudgery of manual conversion between primitive types (such as int, long) and wrapper types (such as Integer, Long).
e.g.      
int number = 1;
Previously
Integer number2 = (Integer) number;

Now in JDK 5.0
Integer number2 = number;

4.      Typesafe Enums - This flexible object-oriented enumerated type facility allows you to create enumerated types with arbitrary methods and fields. It provides all the benefits of the Typesafe Enum pattern ("Effective Java," Item 21) without the verbosity and the error-proneness.
e.g.
Previously
public static final int SEASON_WINTER = 0;
public static final int SEASON_SPRING = 1;
public static final int SEASON_SUMMER = 2;
public static final int SEASON_FALL   = 3;

Now in JDK 5.0
enum Season {WINTER, SPRING, SUMMER, FALL}

5.      Varargs - This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists.
e.g.
Previously
Object[] arguments = {
    new Integer(7),
    new Date(),
    "a disturbance in the Force"
};

String result = MessageFormat.forma("At {1,time} on {1,date}, there was {2} on planet "
     + "{0,number,integer}.", arguments);

Now in JDK 5.0
Using Varargs no need to create object array separately, we can give arguments directy.

String result = MessageFormat.format("At {1,time} on {1,date}, there was {2} on planet "
    + "{0,number,integer}.",
    7, new Date(), "a disturbance in the Force");

6.      Static Import - This facility lets you avoid qualifying static members with class names without the shortcomings of the "Constant Interface antipattern."
e.g.
Previously
double r = Math.cos(Math.PI * theta);

Now in JDK 5.0
import static java.lang.Math.*;
Once the static members have been imported, they may be used without qualification or class names:
double r = cos(PI * theta);

7.      Annotations (Metadata) - This language feature lets you avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code. This leads to a "declarative" programming style where the programmer says what should be done and tools emit the code to do it. Also it eliminates the need for maintaining "side files" that must be kept up to date with changes in source files. Instead the information can be maintained in the source file.
Now in JDK 5.0
public @ interface Test{}





No comments:

Post a Comment