Saturday, September 19, 2009

Changes/Improvements in Java 5: A Breath of Fresh Air

In this post I am going to talk about the changes or better put as improvements in the Java language from version 5 (code named Tiger) released on 29 September, 2004. Although its been a long time since this version was released and even a newer version Java 6 was released in 2006, it was the version 5 since the first release of Java which brought changes in the syntax of the language, there weren't any noticeable language changes in the version 6.

Following is the list of changes introduced in the language in version 5-

Rather than calling them changes its better to call them improvements for two reasons, first of all although the new JDK supports this new syntax, it will still support the old syntax so that old programs and software do not break. Second, this new syntax makes our code more robust, more readable and efficient as well in some cases.

As we see the list of improvements is long enough and its not possible to talk about all of them in this single post, so i will talk about a few of them only right now. Lest start with generics.

Generics: Generics introduce the concept of type parameters to Java. Essentially what this means is that we can write general methods and classes without the knowledge of what type of object will the client use with these methods and classes, these details are deferred until the client declares or instantiates the method or class. This aptly justifies the name of this feature as Generics because it allows us to write even more generic and flexible code as compared to before.

This is a feature which we can use in building our own generic classes and methods and it has been used in the Java 5 class library itself with the collections framework. You all must be familiar with the collections framework in Java.
Prior to version 5 we used to write code like this:

ArrayList list=new ArrayList();
list.add(catObject);
list.add(dogObject);
Cat cat=(Cat)list.get(0);
Dog dog=(Dog)list.get(1);


This type of code can cause a lot of bugs and troubles. We can store any type of object inside the list, there is no type checking and then, when we need to get the items from the list we need to perform type casts, which makes the code cluttered and it can also cause type cast exception if we do not cast the item to the right type.

However, from version 5 the collections framework uses the power of generics. That is, now, we can ensure type safety and eliminate type casting. For example consider the following code snippet:

ArraList<Integer> intList=new ArrayList<Integer> ();
intList.add(someIntegerObject);
//intList.add(new Float(5.2)); this will raise compile time error
Integer i=intList.get(0);

Let me explain each line one by one:

Line 1: It declares an ArrayList which will only store objects of type Integer only. Well, most of the time as a programmer we already know what kind of object we are going to store, so we can use this feature to create such collections in which only one type of object can be stored, the compiler checks for this type safety. This makes our code more readable and eliminates a big cause of bugs as well as the whole type safety mechanism is now performed at compile time.

Line 2 : now we are adding an Integer object to the list, this is very similar to the older version and there is nothing special in it.

Line 3: the 3rd line is commented out, as if compiled, this line will raise a compile time error, with the use of generics the compiler will not allow us to write such buggy code and then later waste our valuable time debugging such hard to find mistakes.

Line 4: And this line is the most important, here we are retrieving an item from the ArrayList but unlike earlier, here we do not need to perform typecasting....all thanks to generics. This also makes our code more readable, less cluttered and of course less prone to bugs.

So you must be having a fair idea by comparing the two versions of the code, how useful generics are. They let us write more flexible, readable and most of all type safe code, which saves a lot of time from debugging. But let me remind you this was how Java developers have applied generics in the collections framework, this by no means itself is generics. We can write our own classes and methods which use the concept of generics and some other programmer could use that code just as we used the ArrayList in the above example. However I am not going to explain how we can do that, as it is quite a complex topic and requires great detail. If you are interested in learning more about generics then you should read this-
Its the official tutorial on generics from Sun. It also explains how to write your own generic classes and methods.

Huh, it took pretty long to explain generics. Well perhaps it will take several posts to explain all of these features. So in the next post we will talk about Enhanced For Loop.
Till then Bye :) .

No comments:

Post a Comment