Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Bart van Kuik
Application Developer
Oracle Corporation
Java 1.5
New Language Features
Java 1.5: Tiger
Less boilerplate code through syntax changes
But no new keywords so compatibility with
existing code, except the enum keyword
Additions to Syntax
Generics
Enhanced for loop
Autoboxing/unboxing
Typesafe enums
Static Imports
Varargs
Metadata
Generics
Also called 'parameterized types', since you
pass a parameter upon declaration and
instantiation:
List<String> lijstMetNamen =
new ArrayList<String>();
// 1.4: runtime error / 1.5: compiler error
lijstMetNamen.add(new Integer(1));
Feature: covariant return types
Enhanced for loop
// Old syntax with iterators:
List list = new ArrayList();
for(Iterator i = list.iterator(); i.hasNext();) {
String s = (String) i.next();
System.out.println("String: " + s);
}
// New syntax
List<String> list = new ArrayList<String>();
for (String s : list) { // do something}
// The following is also possible:
String naam = "Harry";
for (char c : naam.toCharArray()) { // do something }
Autoboxing/unboxing
public static void Cow.milk(Integer liter);
public static void Monkey.throw(int distance);
From primitive to reference and back:
int i = 4;
Cow.milk(new Integer(i)); // 1.4
Cow.milk(i);
// 1.5
Object obj = new Integer(3);
Monkey.throw(((Integer)obj).intValue()); // 1.4
Monkey.throw((Integer)obj);
// 1.5
Boxing null gives a NullPointerException
Typesafe enums
Various disadvantages:
class Kaart {
static final
static final
static final
static final
}
int
int
int
int
HARTEN
KLAVEREN
RUITEN
SCHOPPEN
=
=
=
=
0;
1;
2;
3;
New syntax:
public enum Kaart {
harten, klaveren, ruiten, schoppen
}
Static Imports
Before 1.5: lots of typing work to get a
constant:
ErrorConstants.XML_NOT_VALID
New type of import statement:
import static com.elsevier.ew.ErrorConstants.*;
Both static attributes and methods
Varargs
Pass a variable number of arguments to a
method
Like the C function: printf("value: %d", 15);
Compiler now understands this syntax:
public static int optellen(int args...) {
int resultaat = 0;
for(i = 0; i < args.length; i++)
restultaat = resultaat + args[i];
}
Metadata
Newest addition, separate JSR
Clues for precompilers, testtools, e.d.
C# attribute syntax: [xxx]
Java metadata syntax: @xxx
@test
public static int add(int... params) {
// blah
}
Conclusie
Gone through the JCP
Early access 2.2 is downloadable
Beta and Final release somewhere in 2004
Big names: Joshua Blog, Neil Gafter
QUESTIONS
ANSWERS