Alex Miller polled Java users and a fair number are still using JDK 1.4. This post is intended for those folks: have hope, there is truly useful stuff in Java 5.
In this post, I will mention a couple of my favourites. These have been well-documented, and are not even the 'coolest' features, but my respect for them has been well-earned over the last few months.
BackgroundI've been part of a team that has been moving a project from JDK 1.4 to Java 6, and introducing Hibernate/Postgres to replace (in incremental steps) an existing, legacy framework that works with an OODB. This has been a serious refactoring and fairly risky. My analogy is that of putting in a subway transit system into a modern city: it is a massive job, but will be well worth it.
Ten months later, we appear (knock wood) to be finished with a major version of this migration. I've used the following Java 5 features before, but now I am truly fond of them.
The For LoopWhen I first saw the new for loop, I yawned:
List<String> list = new ArrayList<String>();
// snip
for( String s : list ) {
System.out.println(s);
}
It was a nice bit of syntactic sugar, but I was unimpressed. Now, I can't stand to see an old-style loop. The new loop is so much cleaner. A major bonus for us is that it works with arrays:
String[] array = new String[]{ "abc", "def" };
for( String s : array ) {
System.out.println(s);
}
As we have refactored, the code literally looks washed and waxed afterwards. Very nice.
GenericsWhen I first saw generics in Java, I was unimpressed. With a background in C++ and other languages, I understood the point but felt that Java had lost some of its Smalltalk influence, and that the syntax was plain ugly. When I realized the "wall of erasure" (
as coined by Brian Gilstrap), I was even less enthused:
List<String> list = new ArrayList<String>();
list.add("Cubs swept by Dodgers.");
list.add("any team can have a bad millenium!");
// on a static page, generics allows us to avoid the cast,
// which is boring, and compile-time checking.
// The biggest advantage is in a good IDE
// where this information comes alive:
String first = list.get(0);
Now, I am an absolute fan. Yes, the corner cases are sharp, and the FAQ is very long for a reason. Yes, we may well need reified types to get full generic capabilities.
I don't care: the new static typing of collections
saved our project. Seriously. We were wading through dozens of collections in the legacy code and would have never been able to untangle all the knots without Java generics and Eclipse. I repeat:
generics saved us, and I am grateful.
The UpshotWe shouldn't suspend our critical thinking and skepticism when it comes to new language features. However, we should also believe that people are earnestly trying to add value when new features are proposed. For one, I am going to keep an open mind when it comes to looking at the new features for Java 7+. Who knows: the features which cause us to roll our eyes may well be our future favourites.
ps. This is another post but unit testing and integration testing (with dbUnit) was equally essential to our success. Of the bugs in new code, my guess is that 90% were in code that was not tested.
pps. We have barely used static imports, but Eric has done
some cool things with them. They are underrated, IMO.