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:

  1. Not using generics where the limitations make it too difficult; or
  2. 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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • DZone
  • Ma.gnolia
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati

3 Responses to “Q: What Sucks More Than Java Generics?”

  1. Taylor Says:

    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.

  2. Jason Says:

    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.

  3. Peter Lawrey Says:

    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.

Leave a Reply