The most-awaited Java 2 Standard Edition 1.5 (J2SE1.5) is scheduled to ship its beta in late 2003. This release is code named Tiger. This is a feature release that has a lot more additions to the Java language itself. Tiger is been incubated under the JCP umbrella. JSR 176 outlines the release contents for J2SE 1.5. In this article, we will discuss the core language features that are powerful and desperately wanted by developers for a long time.
Generics—aka C++ Templates
The support for generics (the powerful templates in the C++ world) in Java is the feature the developer community wants most. Generics are commonly referred as parameterized types (type polymorphism) in other object-oriented languages. JSR014 defines the requirements for the Generics. Listing 1 shows how current Java collection examples benefit from generics.
import java.util.HashMap; import java.util.Map; class Employee { public String name; public Employee(String name) { this.name = name; } // Method to convert the objects to Strings. public String toString() { String str = "Name : " + name; return str; } } public class TestWithoutGenerics { public static void main(String args[]) { Map employees = new HashMap(); Employee obj = new Employee("Dr No Generics"); employees.put(obj.name, obj); employees.put(obj, obj.name); // not an error! obj = (Employee)employees.get("Dr No Generics"); // cast required System.out.println(obj.toString()); } } public class TestWithGenerics { public static void main(String args[]) { Map<String, Employee> employees = new HashMap<String, Employee>(); Employee obj = new Employee("Dr Generics"); employees.put(obj.name, obj); //employees.put(obj, obj.name); // an error! obj = employees.get("Dr Generics"); // no cast required System.out.println(obj.toString()); } }
Listing 1: Collections usage with generics.
In Listing 1, we have defined an Employee class. We will use this class to demonstrate the usage of generics. Figure 1 shows the normal usage of HashMap; it has no restrictions on the employee objects that it holds. Figure 2 shows the usage of HashMap; it has a restriction on the employee objects that it holds. The major advantage of generics is that it does strong static type checking.
Figure 1: HashMap without generics.
Figure 2: HashMap with generics.
Collection Filtering
Type checking while filtering a collection is overcome with the usage of generics. In Listing 2, we have a filter method in the FilteringWithoutGenerics class, which accepts a collection that may be heterogeneous. This results in a runtime exception while trying to add an Integer element to a String collection. The compiler cannot enforce that the collection should be homogeneous. If you look at the filter method in the FilteringWithGenerics class, you can see that it is made to accept only a String collection with the use of generics. In this approach, you won’t be able to add an Integer element to a String collection. This type checking is performed at compile time itself.
import java.util.ArrayList; import java.util.List; import java.util.Iterator; import java.util.Collection; public class FilteringWithoutGenerics { static void filter(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) { String s = (String) i.next(); if(s.length() == 5) i.remove(); } } public static void main(String args[]) { List list = new ArrayList(); list.add(new Integer(0)); //runtime exception. list.add(new String("one")); list.add(new String("two")); list.add(new String("three")); filter(list); } } public class FilteringWithGenerics { static void filter(Collection<String> c) { for (Iterator<String> i = c.iterator(); i.hasNext(); ) if (i.next().length() == 4) i.remove(); } public static void main(String args[]) { List<String> list = new ArrayList<String>(); //list.add(new Integer(0)); //compile time error list.add(new String("one")); list.add(new String("two")); list.add(new String("three")); list.add(new String("four")); filter(list); } }
Listing 2: Collection filtering with generics.
Setup for Generics Environment
Download the early access release of the generics prototype from http://developer.java.sun.com/developer/earlyAccess/adding_generics/.
This prototype has a Java compiler that recognizes generics syntax. Also, this distribution has modifications to the existing collections library that support generics (collections.jar).
Extract this distribution to a folder. Set the following environment variables for executing the program shown in Listing 1.
SET J2SE14=PATH_TO_J2SE1.4.1_INSTALL_DIR
For example, SET J2SE14=C:j2sdk1.4.1_02
SET JSR14DISTR=PATH_TO_PROTOTYPE_DIR
For example, SET JSR14DISTR=C:jsr14_adding_generics-1_3-ea
Conclusion
With the introduction of generics in J2SE 1.5, developers will benefit from the strong static type checking that helps them to avoid heterogeneous collections that are not type safe. Generics remove an extra type cast that is a performance overhead. Compile-time errors are less expensive when compared with runtime exceptions, which is a major advantage when your system is performing in a production environment. JSR-014 makes itself into J2SE 1.5 after its inception under JCP five years ago. This feature is a bonus to the developers who are tired of doing type checking in their code.
References
JSR 176—J2SETM 1.5 (Tiger) Release Contents
http://www.jcp.org/en/jsr/detail?id=176
JSR 14—Add Generic Types To The JavaTM Programming Language
http://www.jcp.org/en/jsr/detail?id=014
Generics Prototype for JSR014
http://developer.java.sun.com/developer/earlyAccess/adding_generics/
GJ: A Generic Java
www.dmi.unisa.it/people/costagliola/…/articles/2000/0002/0002a/0002a.htm
About the Author
Arulazi Dhesiaseelan has been working as a Senior Software Engineer at Hewlett-Packard. He has a Master of Computer Applications Degree from the PSG College of Technology, Coimbatore. He has around three years of industry experience. He has been working on various Web Service technologies such as WSDL, UDDI, and SOAP. Currently, he is involved in developing an open service framework for mobile infrastructures. He can be reached at aruld@acm.org.