68
J2SE 5.0 New Features

J2SE1.5

Embed Size (px)

Citation preview

Page 1: J2SE1.5

J2SE 5.0 New Features

Page 2: J2SE1.5

J2SE 5.0 aka JDK 1.5 aka Tiger

Page 3: J2SE1.5

What’s New

• J2SE 5.0 is full of dramatic changes to the java programming language, including new syntax support and JVM enhancements.

• It is considered to be the most revolutionary java release since JDK1.2

Page 4: J2SE1.5

Today

• Generics

• Enhanced for Loop

• Autoboxing \ Unboxing

• Typesafe Enums

• Varargs

• Static Imports

Page 5: J2SE1.5

For Each Statement• A great way to ditch annoying Iterators.

Makes code more elegant and less error prone.

• before

• after

Page 6: J2SE1.5

Spot the Bug

Page 7: J2SE1.5

Fixed Version

Page 8: J2SE1.5

Using the For Each Statement

Page 9: J2SE1.5

Iterating an Array

• The for-each construct can be used to iterate arrays, hiding the index variable instead of the iterator:

Page 10: J2SE1.5

The Iterable Interface• To make your own classes work with this

construct you should implement the Iterable interface:

Page 11: J2SE1.5

Autoboxing and Unboxing

• Autoboxing: – Automatic converting of primitives into wrapper

classes

• Integer x = 10;

• Unboxing – Automatic converting of wrapper classes into

primitives

• double PI = new Double(3.14);

Page 12: J2SE1.5

Autoboxing and Unboxing

for (Integer i = 0;i < new Integer(10);) {i++;

}

Boolean b1 = true;Boolean b2 = true;Boolean b3 = false;Boolean result = (b1 || b2) && b3;

Page 13: J2SE1.5

Example

Page 14: J2SE1.5

Enums

• Previously, the standard way to represent an enumerated type was to use the int Enum pattern:

public static final int WINTER = 0;

public static final int SPRING = 1;

public static final int SUMMER = 2;

public static final int FALL = 3;

Page 15: J2SE1.5

The int Enum Pattern - Problems

• Not TypeSafe – you could add seasons or pass an arbitrary

season

• No Namespace– With other enum types

• Uninformative printed values

• Can not add methods to enum values

Page 16: J2SE1.5

The TypeSafe Enum Pattern

• Create a class with a private constructor. Each enum value is a public static final instance of the class.

• Major problem – cannot be used with switch statements.

Page 17: J2SE1.5

Java 1.5 Enums

enum Season{

WINTER, SPRING, SUMMER, FALL

}

Using the enum…

if (type == Season.WINTER)

System.out.println (…)

Page 18: J2SE1.5

Java Enums are powerful features

• You can add arbitrary methods and fields to enum types, implement interfaces, and enjoy the existing implementation of the Object class methods.

• Enums are Comparable and Serializable

Page 19: J2SE1.5

Adding data to Enums

Page 20: J2SE1.5

Adding data to Enums

• The enum type can be declared with a constructor, and each enum constant is declared with parameters to be passed to the constructor.

Page 21: J2SE1.5

Adding Behavior to Enum Constants

• Using switch statements

Page 22: J2SE1.5

Convert Switch statement to polymorphism

Page 23: J2SE1.5

Enums

• Enums are classes that extend java.lang.Enum.

• Each declared value is an instance of the enum.

• Enum values are public, static and final.

• Enums can be compared with == or equals().

• Implement several methods , ordinal(), compareTo(), valueOf()

Page 24: J2SE1.5

Varargs

• In past releases, if you wanted a method to get an arbitrary number of arguments, it should have been defined to receive an array.

• Alternatively you would have created several overloaded methods.

• Tiger allows variable length argument list for its methods, providing cleaner code with less overloaded methods.

Page 25: J2SE1.5

Varargs

• Varargs is done with the … operator.

• Example. The following constructor

public Person (String name, String details… )

Can be called with many different invocations:

new Person (“Alexander ”); new Person (“Alexander ”, “Bell ”); new Person (“Alexander ”,”Graham”, “Bell ”);

Page 26: J2SE1.5

Varargs

• Accessing the varargs is done like accessing arrays

public int sum (int... numbers) { int result = 0; for (int i = 0;i < numbers.length;i++) { result+= numbers[i]; } return result;}

• Varargs can be used only in the final argument position.

Page 27: J2SE1.5

Varargs• Before:

print( new String[] { “1”, “2”, “3” } );...private static void print( String[] array ) { for ( int j = 0; j < array.length; j++ ) { System.out.println( array [ j ] ); }}

• 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 ] }}

Page 28: J2SE1.5

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);

• As a workaround people often put static members as part of an interface and inherit from that interface.

• This is known as the “Constant Interface Antipattern”

Page 29: J2SE1.5

Static Imports

• Using constants of another class is merely an implementation detail. Declaring that a class implements an interface is an API issue. Implementation details should not leak into APIs.

• The static import construct allows you to access static members of another class without inheriting from the type containing them.

Page 30: J2SE1.5

Static Imports

• members of another class are importedimport static java.lang.Math.PI;

• Once imported they can be used without qualification

double r = cos(PI * theta);

Page 31: J2SE1.5

Example

• BeforeSystem.out.println(“Hello”);System.out.println(“World”);System.err.println(“From”);System.err.println(“Me”);

• Afterimport static java.lang.System.*;out.println(“Hello”);out.println(“World”);err.println(“From”);err.println(“Me”);

Page 32: J2SE1.5

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.

• Use with care: only when frequent access is required. It is suppose to improve readability, and overuse may have an opposite effect.

Page 33: J2SE1.5

Generics

• Generic Types have been widely anticipated by the java community. The first place to see their use is in the collections API.

• With Generics you can provide common functionality with Type-safe Lists, Maps and Iterators for different java types.

• Parameterized Types can also be defined as return types and arguments.

• You can also write your own generic types.

Page 34: J2SE1.5

Before Tiger

• Adding a String to a List

• Must use casting to extract the String

• Potential ClassCastException

List list = new ArrayList();list.add(new String(“Hello”);list.add(new Integer(1));list.add(new ArrayList());

String s = (String) list.get(0);

String s = (String) list.get(1);

Page 35: J2SE1.5

Generics With Tiger

• With Generics you construct real homogenous collections.

• Extracting the contents no longer requires casting

List <String> list = new ArrayList<String>();list.add(“One”);list.add(“Two”);list.add(“Three”);

String one = list.get(0);

Page 36: J2SE1.5

Generics• Using casting the programmer is prone to a run

time error.

• With generics the programmer can express his intent to have a homogenous data type.

• In the example the list was a generic interface that took String as a type parameter.

• The significant difference is that now the compiler can assure the type correctness at compile time. we get improved robustness of our code.

Page 37: J2SE1.5

Parameterized types as Arguments

• public void foo( List<String> names)

Page 38: J2SE1.5

Parameterized types as return types

• public List<String> getNames();

Page 39: J2SE1.5

Parameterized types as parameter types

• public void bar

(Map <String, <List<Integers>>> map);

Page 40: J2SE1.5

Defining Simple Generics

Page 41: J2SE1.5

Defining Simple Generics

Page 42: J2SE1.5

Invocation of Generic Types• List<Integer> = new LinkedList<Integer>();

• You may imagine that List<Integer> stands for a version of List where E has been replaced by Integer:

public interface IntegerList {

void add(Integer x)

Iterator<Integer> iterator();

}

• This intuition is helpful, but also misleading.

Page 43: J2SE1.5

Generics Vs. C++ Templates

• In java Generics, there is no real expansion of code per type invocation.

• There are no multiple copies of the code, not in source, not in binary, and not in memory.

• A generic type declaration is compiled once, and turned into a class file, just like any ordinary class or interface declaration.

Page 44: J2SE1.5

Generics and Subtyping

• Is the following code legal?

List<String> sList = new ArrayList<String>();

List<Object> oList = stringList;

• Is a list of Strings also A list of Objects?

oList.add(new Object());

String string = sList.get(0);

// attempt to assign an Object to a String

Compile time Error

Page 45: J2SE1.5

Generics and Subtyping

• Rule – if B is a subtype of A and G is some generic type declaration then

G<B> is not a subtype of G<A>

• This is probably the most counter intuitive rule of generics.

Page 46: J2SE1.5

Wildcards – The need

• Consider a method that prints all the elements of a collection.

old version

first Attempt with generics

But this version will not work with all Collections,Only with Collection<Object> which is not a super type of all collections

Page 47: J2SE1.5

WildCards

• The super type of all collections is noted as Collection<?>, pronounced “collection of unknown”

Page 48: J2SE1.5

Bounded WildcardsWhat if we want topass List<Circle> instead of List<Shape>

Page 49: J2SE1.5

Upper Bound of the Wildcard

• We say that the method receives a list of unknown type that extends shape.

• Shape is the upper bound of the wildcard.

Page 50: J2SE1.5

Generic Methods

• Consider a method that takes an object array and a collection, and inserts all elements of the array into the collection.

• First attempt:

Page 51: J2SE1.5

Generic Methods

• Just like type declarations, method declarations can be generic.

• Generic Methods are methods parameterized by one or more type parameter.

Page 52: J2SE1.5

Generic Methods• We don’t pass the actual type argument to

the method. It is inferred by the compiler based on the most specific type argument that will make the call type-correct.

Page 53: J2SE1.5

Generic Methods

• For Polymorphic method invocations, i.e. If the return type or other method arguments are not effected by the type parameter T, wildcards should be preferred. Type variables can have upper bounds too!

Wildcards support flexible subtyping

Page 54: J2SE1.5

Generic Methods

• Use generic methods when you wish to express dependencies among the argument types or return types.

• Generic methods and wildcards can also be used in tandem.

Page 55: J2SE1.5

Interoperating with existing code

• How can legacy code and generic code work together?

• When a generic type is used without a type parameter, it’s called a raw type.

• There are many interesting implications of combining generic and legacy code. Not in today's scope.

Page 56: J2SE1.5

Erasure and Translation

• Aliasing a generic list with a raw list.

• At runtime the code acts like this

• Both versions will end with a ClassCastException.

Page 57: J2SE1.5

Erasure and Translation• Generics are implemented as a front-end

conversion called erasure. The generic version is converted into a non generic version.

• All uses of type variables are replaced with upper bound of the type variable, and whenever the resulting code is not type correct, a cast is inserted.

• As a result, the type safety of the Java Virtual Machine are never at risk, even in the presence of unchecked warnings.

Page 58: J2SE1.5

A Generic class is Shared by all its Invocations

List <String> l1 = new ArrayList<String>();

List <Integer> l2 = new ArrayList<Integer>();

System.out.println(l1.getClass() == l2.getClass());

• Returns True!!!

Page 59: J2SE1.5

Casts and instanceOf

• You cannot ask an instance if it is an instance of a particular type

• The runtime system will not check casts such as

Page 60: J2SE1.5

Lower bounded Wildcards• Suppose you create a TreeSet of Strings. TreeSet<String>.• We need to pass it a comparator that compares strings. • But a comparator that compares Objects will also do. In fact

any super class of String is fine to use. • Solution: lower bounded wildcards

Page 61: J2SE1.5

Lower bounded Wildcards

• Consider a method that returns the maximal element in a collection.

• The elements should be comparable, and specifically to each other (to the collection type).

• First Attempt:

Page 62: J2SE1.5

Lower bounded Wildcards

• But consider the following class:

• All elements in the collection are comparable to each other, but the inferred type must be Foo, which does not implement Comparable<Foo>

• T does not have to be comparable exactly to itself, it is only required that it is comparable to one of its superclasses.

Page 63: J2SE1.5

Bounding Rules

• If the API only uses a type parameter as an argument you should take advantage of lower bounded wildcards (? super T)

• If the API only returns T, clients will enjoy the flexibility if you provide them upper bounded wildcards (? extends T)

Page 64: J2SE1.5

Converting Existing Code to Generics

• Naïve conversions may unintentionally modify the API contract. Notice that the non generic version of containsAll could work with any collection, while the generic one will not.

• Should the argument type be restricted? Could we pass a collection of a subtype of E?

• In the case of addAll we should assure that the method works with a collection containing sub types of E

Page 65: J2SE1.5

Converting Existing Code to Generics

• Recall the Collections.max() method

But the erasure of this method is

Which is different then the original signatureof max()

Page 66: J2SE1.5

Giving multiple bounds for a type parameter

• Using the Syntax T1 & T2 & … Tn, a type variable is assigned with multiple bounds and is known to be a subtype of all the listed types.

• The first type mentioned in the bound is used as the erasure of the type variable.

Page 67: J2SE1.5

More Features• Annotations

• Overriding methods return types. (Covariant Typing)

• printf-like formatting methods.

• New default Java look & feel.

• Parallel GC by default on server machines

• Class Data Sharing (CDS)

• Threading issues are boosted in Tiger.

Page 68: J2SE1.5

Getting Started

• Get a J2SE 5.0 supported IDE:

– NetBeans 4.0– IDEA IntelliJ 4.5– “Cheetah” – eclipse 3.0 plugin