Q: What Sucks More Than Java Generics?
A: Java without generics. Yes, there are many problems with the Java generics implementation. And I seriously hope that the implementation is improved in Java 7. However, it occurs to me that the problems can generally be worked around by either:
- Not using generics where the limitations make it too difficult; or
- Using unsafe casts.
In both cases, you are no worse off than you were pre-generics, where you had no choice! Despite their limitations, generics have done a lot to reduce painfully repetitive casting and mysterious interfaces (List of what now?). They also enable abstraction of many extremely common functions without losing type information. In these ways I benefit from generics every day, enough to outweigh the frustrations of erasure.









February 28th, 2008 at 3:49 am
True, I’m afraid the arguments always focus on the library developer, rather than the api consumer. As a consumer, generics are great, you know what you’re getting, less type casting, etc.
February 28th, 2008 at 5:13 am
Hi Taylor,
I think it is also just human nature to take for granted what you’ve got and complain about what you haven’t got. Complaints about the weak points of generics are fine: they are far from perfect. What I don’t like is when the addition of generics to the language is painted as an abject failure. The reality is the current implementation solves a lot of common problems from the pre-5 days, i.e. a step in the right direction.
February 28th, 2008 at 8:19 am
I’m a fan of Generic but there are some things about them I find dumb.
e.g.
Class<Class> clazz = Class.class; // compiles ok.
Class<Class> clazz = Class.class.getClass(); // requires a cast and gets a warning.
Map<?, ?> map;
for(Entry<?, ?> entry: map.entrySet()) // compiles ok.
Map map;
for(Entry entry: map.entrySet()) // requires a cast.
Set set = new HashSet<Integer>(); // ok.
Set<Set> setOfSet = new HashSet<Set<Integer>>(); // requires type erasure.