Language: Java
Compiler version: 1.6
In the below code, am trying to do the following:
- create a
List<String> - add a
String - assign
List<String>to rawList - create a
List<Integer> - assign the raw
ListtoList<Integer> - add an
Integer - retrieve the value using
get()@ indexes 1 & 2 and print them.
All statements are compiling (with warnings) and run fine.
But if I try to loop through the List<Integer> using a for loop, I am getting a ClassCastException. I am just wondering why its allowed me to use list.get() method but not allowing me to iterate over it?
Output: (if I run with un-commented for loop) abcd 200
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at genericsamples.CheckRawTypeAdd.main(CheckRawTypeAdd.java:26)
Here is my code
import java.util.*;
import java.io.*;
class CheckRawTypeAdd
{
public static void main(String[] sr)
{
List<String> list_str = new ArrayList<String>();
list_str.add("abcd");
List<Integer> list_int = new ArrayList<Integer>();
List list_raw;
list_raw=list_str;
list_int=list_raw;
list_int.add(200);
Object o1 = list_int.get(0);
Object o2 = list_int.get(1);
System.out.println(o1);
System.out.println(o2);
//for(Object o : list_int)
//{
// System.out.println("o value is"+o);
//}
}
}
I would consider this a compiler bug in
javac. A checked cast is getting inserted. We can see this usingjavap -c CheckRawTypeAddto disassemble the class (cast is 101; note that I took out some of the unneeded lines of code before compiling, so code points will vary):However, the Java Language Spec (14.14.2) indicates that this cast should be to
Object, notInteger. It starts by defining the terms via the grammar:So in our case,
TypeisObject. Then it goes on to say what this gets translated into:So what’s relevant here is the resolution of
TargetType. This is also defined in the JLS:As
Objectis most certainly a reference type, thenTargetTypeisObjectand so the checked cast should be toObject, notInteger.That this is a bug is further evidenced by others in this thread noting that this problem doesn’t occur if
ecj(Eclipse’s compiler) is used. However, I understand that this would be a low priority bug for the Oracle compiler team since you have to abuse generics to exercise it. One would almost say it’s a feature, not a bug.Follow-up
To give final confirmation that it’s a bug, here’s an existing bug report for this exact issue:
Also, I should note two things. First, the JLS references I gave above were in the latest JLS, and that section has actually changed for Java 7 (in response to this bug!)
Here’s what an enhanced for statement should have translated to for Java 6 and earlier:
As you can see, there is no checked cast specified here. So the bug in
javacwasn’t that it was doing the wrong cast, it was that it was doing any cast at all.Second, in Java 7,
javaccorrectly compiles the code according to the JLS SE 7 spec (which is what I cited above). Thus, the following code works:With a correct cast to
CharSequence, notString. I was using JDK 6 to compile initially, not JDK 7.