You are on page 1of 14

JDK Evolutions

Versions
VERSION JDK 1.1.4 JDK 1.1.5 JDK 1.1.6 JDK 1.1.7 JDK 1.1.8 J2SE 1.2 J2SE 1.2.1 J2SE 1.2.2 J2SE 1.3 J2SE 1.3.1 J2SE 1.4.0 J2SE 1.4.1 J2SE 1.4.2 J2SE 5.0 (1.5.0) J2SE 5.1 Future Releases J2SE 6.0 (1.6.0) J2SE 7.0 (1.7.0) CODE NAME Sparkler Pumpkin Abigail Brutus Chelsea Playground (none) Cricket Kestrel Ladybird Merlin Hopper Mantis Tiger Dragon RELEASE DATE Sept 12, 1997 Dec 3, 1997 April 24, 1998 Sept 28, 1998 April 8, 1999 Dec 4, 1998 March 30, 1999 July 8, 1999 May 8, 2000 May 17, 2001 Feb 13, 2002 Sept 16, 2002 June 26, 2003 Sept 29, 2004 Merged into mustang

Mustang Dolphin

Not yet released Not yet released

JDK Differences
Java is compatible between versions. Old code will run, even without recompilation, on new JVMs, and new code will run on old JVMs as long as you avoid new features, and you use the -target compiler option.

1.1 added a totally new event model, using Listeners. This is the level Microsoft has trapped many of its customers at. 1.2 added ArrayList and other Collections. 1.3 added Swing. 1.4 added assertions and nio. 1.5 added generics, enumerations and annotations

JDK 5.0 Language Features


Generics Adds compile-time type safety to the Collections Framework & eliminates casting during retrieval

Enhanced For Loop

Eliminates the error-proneness of iterators and index variables when iterating over collections and arrays.
This facility eliminates the manual conversion between primitive types and wrappers

Autoboxing / Unboxing

Typesafe Enums

This flexible object-oriented enumerated type facility allows you to create enumerated types with arbitrary methods and fields. It provides all the benefits of the Typesafe Enum Varargs. This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length arguments This facility lets you avoid qualifying static members with class names without the shortcomings of the "Constant Interface antipattern. NA ( Meta Data )

Static import
Annotations

Generics
// Removes 4-letter words from Collection. Elements must be strings ( Traditional ) Public static void expurgate(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) { if (((String) i.next()).length() == 4) { i.remove(); } } } // Removes 4-letter words from Collection. Elements must be strings ( Generics ) Public static void expurgate(Collection<String> c) {
for (Iterator<String> i = c.iterator(); i.hasNext(); ) { if (i.next().length() == 4) { i.remove(); } } } You should use generics everywhere you can. The extra effort in generifying code is well worth the gains in clarity and type safety Set<String> s = new HashSet<String>();

Enhanced For Loop


void cancelAll(Collection<TimerTask> c) { for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); ) { i.next().cancel(); } } void cancelAll(Collection<TimerTask> c) { for (TimerTask t : c) { t.cancel(); } }

Enhanced For Loop ( Contd..)


for (Iterator i = suits.iterator(); i.hasNext(); ) { for (Iterator j = ranks.iterator(); j.hasNext(); ) { sortedDeck.add(new Card(i.next(), j.next())); } } ?? What is the problem in the code ??? // BROKEN - throws Exception!
// Fixed, though a bit ugly for (Iterator i = suits.iterator(); i.hasNext(); ) { Suit suit = (Suit) i.next(); for (Iterator j = ranks.iterator(); j.hasNext(); ) { sortedDeck.add(new Card(suit, j.next())); } }

Enhanced For Loop ( Contd..)


for (Suit suit : suits) { for (Rank rank : ranks) { sortedDeck.add(new Card(suit, rank)); } } ** The

for-each loop hides the iterator. Therefore, the for-each loop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. **

// Returns the sum of the elements of array int sum(int[] a) { int result = 0; for (int i : a) result += i; return result;
} **

The for-each construct is also applicable to arrays, where it hides the index variable rather than the iterator **

Autoboxing / unboxing
Boxing means wrapping primitives using their Wrapper Objects // Prints a frequency table of the words on the command line
public class Frequency { public static void main(String[] args) { Map<String, Integer> m = new TreeMap<String, Integer>(); for (String word : args) {

Integer freq = m.get(word); m.put(word, (freq == null ? 1 : freq + 1)); } System.out.println(m);

} }

java Frequency if it is to be it is up to me to do the watusi {be=1, do=1, if=1, is=2, it=2, me=1, the=1, to=3, up=1, watusi=1}

Autoboxing / Unboxing (Contd..)


// List adapter for primitive int array public static List<Integer> asList(final int[] a) { return new AbstractList<Integer>() { public Integer get(int i) { return a[i]; } // Throws NullPointerException if val == null public Integer set(int i, Integer val) { Integer oldVal = a[i]; a[i] = val; return oldVal; } public int size() { return a.length; } }; } The performance of the resulting list is likely to be poor, as it boxes or unboxes on every get or set operation. It is plenty fast enough for occasional use, but it would be folly to use it in a performance critical inner loop.

Enums
// int Enum Pattern - has severe problems! ( Traditional ) public static final int SEASON_WINTER = 0; public static final int SEASON_SPRING = 1; public static final int SEASON_SUMMER = 2; public static final int SEASON_FALL = 3; Not typesafe - Since a season is just an int you can pass in any other int value where a season is required. No namespace - You must prefix constants of an int enum with a string (in this case SEASON_) to avoid collisions with other int enum types. Brittleness - Because int enums are compile-time constants, they are compiled into clients that use them. If a new constant is added between two existing constants or the order is changed, clients must be recompiled. If they are not, they will still run, but their behavior will be undefined. Printed values are uninformative - Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is. // new way enum Season { WINTER, SPRING, SUMMER, FALL } private final Season _season ; Season.values()) The new enum declaration defines a full-fledged class add arbitrary methods and fields to an enum type implement arbitrary interfaces

Varargs
public static String format(String pattern, Object... arguments);

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position.

System.out.printf("%s failed: %s%n", className, ex);

Static Import
// Traditional Way double r = Math.cos(Math.PI * theta); // New way import static java.lang.Math.*; double r = cos(PI * theta);

Miscellaneous
java.lang.ProcessBuilder : provides a more convenient way to invoke subprocesses than does Runtime.exec. In particular, ProcessBuilder makes it easy to start a subprocess with a modified process environment java.util.Formatter : An interpreter for printf-style format strings, the Formatter class provides support for layout justification and alignment, common formats for numeric, string, and date/time data, and locale-specific output java.util.Scanner class can be used to convert text into primitives or Strings. Since it is based on the java.util.regex package, it also offers a way to conduct regular expression based searches on streams, file data, strings Task Scheduling Framework - The Executor framework is a framework for standardizing invocation, scheduling, execution, and control of asynchronous tasks according to a set of execution policies. Implementations are provided that allow tasks to be executed within the submitting thread, in a single background thread (as with events in Swing), in a newly created thread, or in a thread pool, and developers can create of Executor supporting arbitrary execution policies. The built-in implementations offer configurable policies such as queue length limits and saturation policy which can improve the stability of applications by preventing runaway resource consumption public void addShutdownHook(Thread hook) Registers a new virtual-machine shutdown hook. The Java virtual machine shuts down in response to two kinds of events: The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

You might also like