Presents your JAVA E-NEWSLETTER for June 5, 2003 <-------------------------------------------> TAKE ADVANTAGE OF GENERICS WITH THE GJ COMPILER Although generics are still not part of the standard Java distribution, they are available from a third-party compiler named Generic Java (GJ). GJ is a subproject of the open source Pizza compiler project and is being used as the model for generics in Java 1.5. The easiest way to explain generics might be with an example. In Java, if you were manipulating java.util.List full of java.lang.String objects, you would have code that looks something like this: ... // create a list and some string objects to it List strings = new ArrayList(); strings.add("one"); strings.add("two"); strings.add("third string"); strings.add("and a fourth for good measure"); ... Later in your application, you may want to access a reference to one of the strings, where foo is a method that accepts a String object as its only argument. ... String s = (String) strings.get(0); foo(s); ... Most Java programmers are so used to seeing this code that they may not notice the potential problems. Hint: What keeps you from adding an instance of java.lang.Integer to your list like this: strings.add(new Integer(5)); Answer: nothing. The collections containers accept and return instances of java.lang.Object. That's why the assignment to "s" above requires a cast. You won't know that you accidentally added a java.lang.Integer instance to your list until runtime, when an attempt is made to assign the Integer instance to "s." You could solve this problem by writing your own list class that accepts and returns only String objects, but that could become painful quickly. Generics provide the functionality you need and with no new code--just new syntax. Here's a complete file containing the above snippets implemented with generics: import java.util.ArrayList; public class GenericsTip { public static void main(String args[]) { ArrayList strings = new ArrayList(); strings.add("one"); strings.add("two"); strings.add("third string"); strings.add("and a fourth for good measure"); foo(strings.get(0)); } public static void foo(String arg) { System.out.println("arg: " + arg); } } The most obvious difference between the two implementations is the declaration of the ArrayList object strings. Instead of creating an ArrayList that will contain and return instances of Object, the generics syntax is used to create a list that may contain and return only String objects. If an attempt is made to insert an Integer into the list of String objects, a compile time error is thrown, like this: GenericsTip.java:10: cannot resolve symbol symbol : method add (java.lang.Integer) location: class java.util.ArrayList strings.add(new Integer(5)); ^ Creating the List using generics allows you to strongly type your collections and avoid class cast errors and other potential pitfalls at runtime without imposing too much overhead. To compile the source file above, you'll need the GJ compiler. http://cl.com.com/Click?q=35-d6-TIbIJbyDx1vSLcTmAG4yLc3e2 ----------------------------------------