JDK 1.5 Features

1.Generics
Allows programmers to specify the types allowed for Collections
Allows the compiler to enforce the type specifications
//Before
List stringList
//In JDK 1.5
List<String> stringList;
2.Enhanced for loop
A new language constuct for the Iterator pattern
//Before
for(Iterator i = line.iterator();i.hasNext(); )
{
  String word = (String)i.next();
  …
}
//In JDK 1.5
for(String word: line)
{
  …
}
3.Autoboxing
Essentially bridges between the “primitive” types (such as int, boolean) and the
“boxed” types (such as Integer, Boolean)

int primitive = 5;
Integer boxed = primitive;
int unboxed = boxed + 5;

4.Varargs
Allow a variable number of arguments for methods like printf() or Method.invoke()
Internally parameters are an array of Object
Compiler constructs array for the call
void printf(String format, Object…args);

printf(“{0} {1}\n”, “Hello”, “World”);
printf(“PI = {0}”, 3.14159);
5.Enumerations
Essentially the “typesafe enum pattern”.
Simple C-style enumeration
Can add behavior to each instance
High peformance EnumSet implementaion using a bit-vector

public enum Coin {
PENNY(1), NICKEL(5);
private final int value;
Coin(int value) {this.value = value}
}

6.Metadata
Ability to decorate Java classes and their members with arbitrary data
Retrieve values
– From source files
– From class files
– At runtime using Reflection
@Retention(RetentionPolicy.RUNTIME)
public @interface Test{}