Tuesday, September 23, 2008

New features of Java 5 (Java Tiger/J2SE 5.0/Jdk 1.5)

Following section gives a quick introductory of the new features of Java 5

Generics:
Provides compile time type-safety.
 o  Example: List<String> myList= new ArrayList<String>(); where myList is an array list of String type.

Enhanced for loop:
For statement can be used to iterate over an array or a collection.
 o  Example: for(Object object: myList) where myList if an ArrayList collection and its elements are assigned to object.

Autoboxing/unboxing:
Automatic conversion of wrapper types to primitive types and vice versa.
 o  Example: boxing: int i=new Integer(42);
 o  Example: unboxing: Integer iObj=36;

Enum type:
A new enumeration type in java
 o  Example: private enum CustomerType {Individual, Corporate}; later can be referenced as CustomerType.Individual or CustomerType.Corporate.

Static import:
Provides a way to access final static fields without referencing it with class name.
 o  Example: import static java.util.Calendar.SATURDAY; later can be directly referenced as SATURDAY instead of Calendar.SATURDAY.

Varargs:
Allows methods to have variable length of arguments. It can be used instead of array.
 o  Example: public static void main(String... args) where … says that there is 0 or more arguments.

Annotations:
Provides notes in java programs to ask java compiler to do something.
 o  Example: @Override, @Deprecated

Reference links:
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

No comments: