J2SE 5.0

Generics
This long-awaited 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. Refer to JSR 14.

Example :

import java.util.*;

public class Ex2 {

private void testCollection() {
List list = new ArrayList();
list.add(new String(“Hello world!”));
list.add(new String(“Good bye!”));
list.add(new Integer(95));
printCollection(list);
}

private void printCollection(Collection c) {
Iterator i = c.iterator();
while(i.hasNext()) {
String item = i.next();
System.out.println(“Item: “+item);
}
}

public static void main(String argv[]) {
Ex2 e = new Ex2();
e.testCollection();
}

Enhanced for Loop
This new language construct eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays. Refer to JSR 201 .
Syntax : for (FormalParameter : Expression) Statement
Example :

// Assume we have an instance of StringBuffer “sb”
public void oldFor(Collection c) {
for(Iterator i = c.iterator(); i.hasNtext(); ) {
String str = (String) i.next();
sb.append(str);
}
}

Autoboxing/Unboxing
This facility eliminates the drudgery of manual conversion between primitive types (such as int) and wrapper types (such as Integer). Refer to JSR 201 .
Old code :

list.add(0, new Integer(59));
int n = ((Integer)(list.get(0))).intValue();

New autoboxing/unboxing feature :

list.add(0, 59);
int total = list.get(0);

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. Refer to JSR 201.

Varargs
This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists. Refer to JSR 201.

The variable arguments new functionality in J2SE 5.0 allows multiple arguments to be passed as parameters to methods as in:

void someMethod(Object … args) {
// do something
}
// invoke the method
someMethod(“arg1″, “arg2″, “arg3″);

Static Import
This facility lets you avoid qualifying static members with class names without the shortcomings of the “Constant Interface antipattern.” Refer to JSR 201.

Example :

package com.name;

interface XYZ {
public static final double Constant1 = someValue;
public static final double Constant2 = anotherValue;
}

Now, the constants in the XYZ interface can be used as follows:

public class MyClass implements XYZ {
….
double value = 2 * Constant1;

}

In J2SE 5.0, static import solves this problem as shown in the following example:

import static com.name.XYZ.*;
public class MyClass {

double value = 2 * Constant1;

}

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. Refer to JSR 175.

The following are some of the enhancements made for improved program execution speed in JDK 5.0.

Garbage collection ergonomics
Provides for the automatic detection and choice of the client or server runtime compiler, which enhances performance on server-class machines. See “Server-Class Machine Detection” and “Garbage Collection Ergonomics” at Java Virtual Machines for more information.

StringBuilder class
The addition of a new class StringBuilder that works essentially as an unsynchronized StringBuffer for performance enhancement. You should replace all StringBuffer uses with StringBuilder unless you need the synchronization (which you almost certainly don’t). StringBuilder is almost always faster than StringBuffer.

Java 2D technology
These Java 2D performance enhancements have spin-off performance benefits in other areas of functionality such as Swing/JFC. Examples include improved acceleration for BufferedImage objects, support for hardware-accelerated rendering using OpenGL, and improved text-rendering performance. See New Java 2D Features for more information.

Image I/O
Performance and memory usage improvements have been made when reading and writing JPEG images. See “Improved Performance” at Image I/O Features.

Leave a Reply