Card 03/ 09
All 9 cards
ConceptDifficulty: Intermediate1 min
What Is Left of List<String> by the Time the Program Runs
Two variables, declared with different type arguments, and a comparison of their classes that says they are the same.
List<String> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
System.out.println(a.getClass() == b.getClass());trueType arguments are checked by the compiler and then erased. The bytecode contains one ArrayList class, holding Object references, exactly as it did before generics existed.
| Written | At compile time | At runtime |
|---|---|---|
List<String> | A list that only accepts strings | A list |
refs.get(0) | Produces a String | Produces an Object, cast to String |
T in a generic class | A real type | Object |
Erasure was chosen so that code written before Java 5 kept running unchanged on the same virtual machine. It worked, and the price is paid by every strange generics rule you will meet.
So when the compiler refuses something about generics that looks perfectly reasonable, the reason is nearly always that the information you are relying on is gone by the time it would be needed.