All Hail Java 5

If you do any Java development and are stuck with Java 1.4, my condolences.  Java 5 is so amazingly better, and in relatively inconspicious ways.  For those of you not in the know, here's a highlight:

  • Generics: I've been waiting for these in Java since my undergraduate days down at the University of Arizona.  Roughly analogous to templates in C++, they let you define classes and methods as operating on a dynamic type.  For example, the Collections framework used to operate entirely on java.lang.Objects, but now you can create dynamic collections for any type, and avoid the casting hell that plagued pre 5.0 Java code.
  • Autoboxing: Java, like many other OO languages differentiates objects from primatives.  With 5.0, the line got very blurry, because the compiler/VM will take care of converting primitives to and from their corresponding object types.  Very handy if, for example, you need a java.util.Set of ints, and don't want to have to fill your otherwise clean code with a bunch superfluous java.lang.Integer objects.  Much nicer to let the compiler/VM handle the legwork.
  • Annotations: Also called metadata, they're a language construct that allows embedding non-code information directly in your source.  Think Javadocs, but on steriods.  JUnit uses annotations extensively in the latest version, as does Hibernate (a la EJB 3), Spring, and a whole host of other Java frameworks.  Storing metadata directly in your source had it's downsides (harder for non-technical people to edit), but the benefits are huge because that data is available at all points from development, through testing, and at runtime to help tools make your job easier.
  • Real enumerations: Until Java 5, if you wanted enumerated constants (thing suits of a deck of cards), you had to manually declare constants for each one that were totally independant.  With Java 5 you can declare an enum (which is a type just like a class or interface), that encapsulates your constants in a single cohesive package with automatic value differentiation (i.e. no chance of accidentally assigning the same value to multiple constants), an actual type (rather than piggybacking on int or String), and perhaps most interestingly of all, behaviour.  There are also some high-performance collections available just for enums that leverage implementation details in the VM, but shield the details from the developer.
  • New for loop: Java 5 now has a "for in" loop like many other languages possess.  In Java, it only works with java.lang.Iterable instances, which all the collections are, and you can create your custom classes as.  The "for in" syntax is nothing special, but it hides the iterator, and best of all, since java.lang.Iterable is generic, you get to skip iteration details AND casting.

There are a couple other features (importing static members and variable argument lists), but those are of generally less interest.  One thing that I found interesting was the massive focus on more readable and maintainable source code at the expense of both compile-time and runtime performance.  One more (substantial) argument that readable code is better than fast code, though having both is obviously ideal.

The one thing they missed that I really would have liked to see is implict accessor methods.  Actionscript 3 and C# both have these (as ECMAScript-y langauges).  The reduction of boilerplate code is huge, because you can just throw all your properties public and only encapsulate the ones that need it.  Most importantly, you get to make that encapsulation decision at any time without affecting your object's interface.  Hopefully in whatever the heck the next version of Java is (Java2 5.0 5.1 or something).

So if you're still doing Java 1.4 (or older) development, it's definitely worth your time to upgrade ASAP.

Comments are closed.