Professional Documents
Culture Documents
J2SE 5.0 New Features
J2SE 5.0 New Features
0 New Features
J2SE 5.0 aka JDK 1.5 aka Tiger
What’s New
• J2SE 5.0 is full of dramatic changes to the
java programming language, including
new syntax support and JVM
enhancements.
• after
Spot the Bug
Fixed Version
Using the For Each Statement
Iterating an Array
• The for-each construct can be used to
iterate arrays, hiding the index variable
instead of the iterator:
The Iterable Interface
• To make your own classes work with this
construct you should implement the
Iterable interface:
Autoboxing and Unboxing
• Autoboxing:
– Automatic converting of primitives into wrapper
classes
• Integer x = 10;
• Unboxing
– Automatic converting of wrapper classes into
primitives
Boolean b1 = true;
Boolean b2 = true;
Boolean b3 = false;
Boolean result = (b1 || b2) && b3;
Example
Enums
• Previously, the standard way to represent
an enumerated type was to use the
int Enum pattern:
• No Namespace
– With other enum types
if (type == Season.WINTER)
System.out.println (…)
Java Enums are powerful features
• After:
print( “1”, “2”, “3”, “4” ); // put as many as you need
...
private static void print( String ... array ) {
System.out.println ( array.length );
for ( String s : array ) {
System.out.println( s ); // same as array[ j ]
}
}
Static Imports
• In order to access static members, it is
necessary to qualify references with the class
they came from.
double r = Math.cos(Math.PI * theta);
• After
import static java.lang.System.*;
out.println(“Hello”);
out.println(“World”);
err.println(“From”);
err.println(“Me”);
Static Vs. Normal import
• Static imports are analogous to regular imports.
In normal imports you import classes from
packages, and in static imports you import static
class members out of classes.
• Potential ClassCastException
String s = (String) list.get(1);
Generics With Tiger
• With Generics you construct real
homogenous collections.
List <String> list = new ArrayList<String>();
list.add(“One”);
list.add(“Two”);
list.add(“Three”);
oList.add(new Object());
String string = sList.get(0);
// attempt to assign an Object to a String
Generics and Subtyping
• Rule – if B is a subtype of A and G is
some generic type declaration then
• First attempt:
Generic Methods
• Just like type declarations, method
declarations can be generic.
• Returns True!!!
Casts and instanceOf
• You cannot ask an instance if it is an
instance of a particular type
• First Attempt:
Lower bounded Wildcards
• But consider the following class:
– NetBeans 4.0
– IDEA IntelliJ 4.5
– “Cheetah” – eclipse 3.0 plugin