2007-08-17

bolson: (Default)
2007-08-17 08:00 am
Entry tags:

Java ate my brain

Java's generics are hard. And I think they're defined wrong.
HashMap<String,String>[] arrayOfHashMap; // Ok, I can declare this
arrayOfHashMap[0] = new HashMap<String,String>(); // this compiles just fine
HashMap<String,String> instanceOfHashMap = arrayOfHashMap[0]; // this works fine in code too
arrayOfHashMap = new HashMap<String,String>[]; // but this always is a compiler error!
arrayOfHashMap = new HashMap[]; // and this is a warning! I can't win!

This is clearly a bug in the design of the language.

Also, ObjectInputStream.readObject(), which returns Object, appears to have no way of assigning to a templated type without warning without adding the annotation @SuppressWarnings({"unchecked"}) to the function. Hmpf. Shoulda stuck with Java 1.4