Saturday, February 19, 2005

The coffee gets its Tiger

I've been programming with Java 5.0 for several weeks now, and I'm much more impressed with it than I thought I'd be. I expected to find some interesting stuff that wasn't worth the risks (real or perceived) in switching from more mature versions of Java. After just the short experience I've had, I'd recommend that any sizable Java development effort seriously consider switching to Java 5.0. In my opinion, the benefits are so great that they outweigh those risks.

So what's so great about Java 5.0? Well, my favorite new features are generics (very similar to templates in C++), autoboxing (which lets you use basic types even where objects are ordinarily required) and the new for construct that virtually eliminates the need to explicitly instantiate iterators (or the ubiquitous i iteration index). While there are many more things to love about Java 5.0, I'm going to pick on these three and talk briefly about each of them, with some short examples.

Generics. What can I say? They're great! Below is an example of generics used to make a list of strings:

    // here's what you used to do...    List<String> myList = new ArrayList();    myList.add( "test" );    myList.add( new Integer(0) );   // compiles and runs just fine!    String gotIt = (String) myList.get(0);    // and here's what you do with generics...    List<String> myList = new ArrayList();    myList.add( "test" );    myList.add( new Integer(0) );  // won't compile -- sweet!    String gotIt = myList.get(0);

This one feature has greatly reduced the tediousness of using collections, reduced the errors in my code, and increased the readability. And I haven't even started writing my own generified classes yet!

Autoboxing. Autoboxing is a lovely thing. Java 5.0 automatically converts basic types to their equivalent object type when needed, and vice versa. This saves no end of tedium while writing code, and results in much more readable code. For example, the following code would have not have compiled pre-Java 5.0, but now it works fine (and as expected!):

    // before you'd do this...    int x = 5;    Integer y = new Integer(5);    x = y.intValue();    // now you just do this...    int x = 5;    Integer y = x;  // autoboxing instantiates the Integer...    x = y;  // autoboxing does the Integer.toValue()... 

for. With Java 5.0, there's no need to instantiate trivial iterators or index integers. The compiler does all the work for you. An example:

    // before you'd do this...    Iterator it = myCollection.iterator();    while( it.hasNext() ) {        // do stuff...    }    // now you do this...    for( Object o : myCollection ) {        // do stuff...    }    // or before you'd do this...    for( int i = 0; i < myArray.length; i++ ) {        // do stuff...    }    // now you do this...    for( Object o : myArray ) {        // do stuff...    }

Of course you can combine the new for syntax with generics, and that leads to much simpler and more readable code, and less prone to stupid typo errors as well. How many times have you typed that for( int i = 0;... pattern? It feels like about a million times for me! I'm very glad to leave that particular construct behind...

No comments:

Post a Comment