In what circumstances can ClassCastException occur in the code below:
import java.util.Arrays;
import java.util.List;
public class Generics {
static List getObjects() {
return Arrays.asList(1, 2, 3);
}
public static void main(String[] args) {
List<String> list = getObjects();
for (Object o : list) { // ClassCastException?
System.out.println(o);
}
}
}
We had a similar case in a production environment (bad practice, I know) and the customer provided a log with ClassCastException at the line with the comment but I can’t seem to reproduce it. Any thoughts?
I know that the JVM creates an iterator in the background when using foreach, but can it create a raw Iterator in some cases and a parametrized one in other cases?
Update:I also had a look at the bytecode generated and on Windows, using JDK 1.6.0_21-b07 no checkcast was made. Interesting 🙂
Here is the main method:
public static void main(java.lang.String[]); Code: 0: invokestatic #34; //Method getObjects:()Ljava/util/List; 3: astore_1 4: aload_1 5: invokeinterface #36, 1; //InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 10: astore_3 11: goto 28 14: aload_3 15: invokeinterface #42, 1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 20: astore_2 21: getstatic #48; //Field java/lang/System.out:Ljava/io/PrintStream; 24: aload_2 25: invokevirtual #54; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V 28: aload_3 29: invokeinterface #60, 1; //InterfaceMethod java/util/Iterator.hasNext:()Z 34: ifne 14 37: return
Thanks all for the answers!
Update 2: I got mislead with Eclipse IDE that uses its own compiler so actually the bytecode above it’s the one generated using the Eclipse compiler. Look here how to manually compile code with Eclipse.
In conclusion Eclipse compiler generates different byte-code from the Sun compiler in some cases, regardless of the platform, the case described here being one.
Shouldn’t that code always throw a
ClassCastException? It does for me using the Sun Java 6 compiler and runtime (on Linux). You’re castingIntegers asStrings. The iterator created will be anIterator<String>, but then it tries to access the first element, which is anInteger, and so it fails.This gets clearer if you change your array like so:
Now the loop actually works for the first element, because the first element is a
Stringand we see the output; then theIterator<String>fails on the second one, because it isn’t a string.Your code works if you just use a generic
Listinstead of a specific one:…or, of course, if you use
List<Integer>, since the contents areIntegers. What you’re doing now triggers a compiler warning —Note: Generics.java uses unchecked or unsafe operations.— and for good reason.This modification also works:
…presumably because at that point you’re dealing with an
Iterator, not anIterator<String>.bozho has said he doesn’t see this error on Windows XP (didn’t mention which compiler and runtime, but I’m guessing Sun’s), and you say you’re not seeing it (or not reliably), so clearly there’s some implementation sensitivity here, but the bottom line is: Don’t use
List<String>to interact with aListofIntegers. 🙂Here’s the file I’m compiling:
Here’s the compilation:
Here’s the run:
tjc@forge:~/temp$ java Generics one Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at Generics.main(Generics.java:12)Line 12 is the
forstatement. Note that it did output the first element, because I changed that to aString. It didn’t output the others. (And before I made that change, it failed immediately.)Here’s the compiler I’m using:
Here’s the disassembly, which shows the
checkcast:tjc@forge:~/temp$ javap -c Generics Compiled from "Generics.java" public class Generics extends java.lang.Object{ public Generics(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."":()V 4: return static java.util.List getObjects(); Code: 0: iconst_3 1: anewarray #2; //class java/io/Serializable 4: dup 5: iconst_0 6: ldc #3; //String one 8: aastore 9: dup 10: iconst_1 11: iconst_2 12: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 15: aastore 16: dup 17: iconst_2 18: iconst_3 19: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 22: aastore 23: invokestatic #5; //Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List; 26: areturn public static void main(java.lang.String[]); Code: 0: invokestatic #6; //Method getObjects:()Ljava/util/List; 3: astore_1 4: aload_1 5: invokeinterface #7, 1; //InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 10: astore_2 11: aload_2 12: invokeinterface #8, 1; //InterfaceMethod java/util/Iterator.hasNext:()Z 17: ifeq 40 20: aload_2 21: invokeinterface #9, 1; //InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 26: checkcast #10; //class java/lang/String 29: astore_3 30: getstatic #11; //Field java/lang/System.out:Ljava/io/PrintStream; 33: aload_3 34: invokevirtual #12; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V 37: goto 11 40: return }Again, though, the bottom line has to be: Don’t use a
List<String>to interact with aListthat contains things that aren’tStrings. 🙂