J2SE 5.0 (or JDK 5), codenamed Tiger, was released on September 30, 2004. It was renumbered to 5.0 instead of 1.5.
JDK 5 New Language Features
JDK 5 is a major update, which introduces many important new language features.
Generics (JSR 14)
Provide compile-time type-safety for Collection
and eliminate the need for most type-cast. See "Generics".
Annotations (JSR 175)
Annotations (or metadata) allow classes and methods to be tagged with additional data, which can then be pre-processed by the compiler. See "Annotations".
Autoboxing/unboxing (JSR 201)
Automatic conversion between primitive types (such as int
, double
) and primitive wrapper classes (such as Integer
, Double
). See "Java Collection Framework".
Enumerations (JSR 201)
The new keyword enum
creates a type-safe, ordered list of values. See "Enumerations".
Varargs (Variable Arguments)
The last method's formal parameter can be declared using a type name followed by three dots (e.g., String... messages
), which accepts any numbers of actual parameters packed in an array. For example,
public class JDK5TryVarargs { public static void main(String[] args) { greets("Hello", "Peter", "Paul"); } public static void greets(String greeting, String... names) { System.out.print(greeting + ","); // varargs received as an array for (int i = 0; i < names.length; ++i) { System.out.print(" " + names[i]); } System.out.println(); } }
Enhanced for-each loop: for ( : )
New construct for iterating thru a Java Collection
. For example,
import java.util.List; import java.util.ArrayList; public class JDK5TryForEach { public static void main(String[] args) { int[] marks = {11, 22, 33, 44, 55}; // array // JDK 5's new for-each loop for (int mark : marks) { System.out.println(mark); } List<String> fruits = new ArrayList<>(); // Collection fruits.add("apple"); fruits.add("orange"); for (String fruit : fruits) { System.out.println(fruit); } } }
static import Statement
For importing members (fields and methods) as public static
. Can be referenced without specifying the class name.
import static java.lang.Math.PI; // import a constant in a class public class JDK5TryStaticImport { public static void main(String[] args) { System.out.println(PI); // No classname needed } }
JDK 5 Library Changes
[TODO]
JDK 5 Other New Features
[TODO]
REFERENCES & RESOURCES