Previous | Home | Next |
Java 2 Platform Standard Edition 5.0 is a major feature release. The features listed below are introduced in 5.0 since the previous major release (1.4.0).
-
Generics: provides compile-time type safety for collections and eliminates the need for most type conversion.
Here is an example how we can use generics in our code.
old syntax List myList = new ArrayList(); new syntax List<String> myList = new ArrayList<String>();
-
Enhanced for Loop: The for loop syntax is extended with special syntax for iterating over each member of either an array or any iterable collection.
Here is an example how we can use enhanced for loop in our code.
//old syntax String[] myStrings = new String[10]; for(int i=0; i<10; i++) { myStrings[i] = "Count " + i; } //new syntax String[] myStrings = new String[10]; for(String str : myStrings) { str = "Count " + i; }
-
Autoboxing/Unboxing: Automatic conversions between primitive types (such as int) and primitive wrapper classes (such as integer).
Here is an example how autoboxing/unboxing works
List<Integer> myIntList = new ArrayList<Integer>(); int maxNumber = 10; myIntList.add(maxNumber);
If you notice here myIntList is accept only Integer objects, but here we can add int primitive type variable as well. it internally converts int(primitive type) to Wrapper class(Integer), Its called Autoboxing.
int minNumber = myIntList.get(0);
-
Static Import: This facility lets you avoid qualifying static members with class names without the shortcomings of the "Constant Interface anti-pattern."
Suppose you want to use DATE constant from the Calender class in your code at multiple places then you have to first import the Calender class in your program using.
import java.util.Calendar;
-
Metadata (Annotations): 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.
There are so many annotation you can use in your program like @Before, @BeforeClass, @Override, @Deprecated, @Test etc.
-
Enumerations: The enum keyword creates a typesafe, ordered list of values (such as day.monday, day.tuesday, etc.). Previously this could only be achieved by non-typesafe constant integers or manually constructed classes.
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
-
Varargs: In past releases, a method that took an arbitrary number of values required you to create an array and put the values into the array prior to invoking the method.
Example, Here is how one used the MessageFormat class to format a message:
Object[] arguments = { new Integer(7), new Date(), "a disturbance in the Force" }; String result = MessageFormat.format( "At {1,time} on {1,date}, there was {2} on planet " + "{0,number,integer}.", arguments);
-
Concurrent API: concurrency utilities in package java.util.concurrent. like ConcurrentHashMap,ConcurrentLinkedQueue etc.
-
StringBuilder class: StringBuilder class (in java.lang package).
Previous | Home | Next |