Java Generics

I have to admit, I’m a little late to the “Java 5” party. My last project was stuck on WAS 5.something and we just barely got to Java 1.4 before I wandered off for greener pastures. Of course about this time last summer I started getting a lot more interested in Ajax, Ruby, Rails, OS X and whatnot so I just didn’t pay that much attention to the newest features.

These days, my application *requires* 1.5.06 and we are highly encouraged to take advantage of the newest language features. Now I’ll admit, even when I was mired in 1.4 land, I was pretty intrigued by generics and with that in mind, I was pretty interested in Venkat Subramaniam’s talk on the topic when the No Fluff tour swung into town last year. Without a doubt, Venkat’s talk was one of the highlights of the show and I was left feeling pretty good about our (admittedly pre 1.5) approach of just wrapping a collection and typing the methods we wanted to expose (this gave us some side benefits beyond type safety - we could create methods like getFooAfterDate, we could limit the API, and we could program more defensively if we needed to).

Anyway, I’ve been getting into quite a few debates these days about using generics where I seem to be the lone voice against (I am buoyed by Bruce Tate’s comments in Beyond Java along with Venkat’s articles: part 1, part 2, part 3). Some of our technical leaders thing generics are the greatest thing since sliced bread and while I don’t agree, I did find myself using them the other day on an interface I was creating (I know, I still feel a little…dirty). While I’m clearly not going to change too many minds, I have to admit, running into Java Generics FAQ: 433 pages… on murphee’s Rant (which links to Angelika Langer’s Java Generics FAQ) really got my attention. I mean, hey, generics are a good concept, but any implementation that inspires a FAQ of this length? Wow, that’s hard to fathom.

Now, I’ll admit, I’ve never really liked typing the casts but I’m not so sure that List<Type> myList = new ArrayList<Type>() is a huge step forward (and just read a bit about type erasure). Hey, I’m all for better code but I’ve just got to ask - when was the last time you got a class cast exception in your production code?

Leave a Reply