Arun.V.Somasundaram
Note: TestList1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Arun.V.Somasundaram
I assume that in the second case, the compiler ("knowing" that he has a list of Integers) just creates code that uses the "toString()" method of the objects in the list. So the code will run without problems.
For the first case? Again assuming, the compiler expects String objects, so he doesn't use the toString() method. Then the virtual machine finds that the object type is in fact not String. Integer is not convertible to String so you get the exception.
Arun.V.Somasundaram
In the second case the compiler creates code that just takes Object elements from the list, and passes them to the PrintStream.println(Object) method
Arun.V.Somasundaram
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Arun.V.S wrote:In the second case why the compiler doesn't tries to make sure it actually has Integer objects with the checkcast instruction since the <Integer> generic type is used.
In the first case why the compiler tries to make sure it actually has String objects with the checkcast instruction. If the compiler does make sure about the type of the objects in the first case it must also make sure about the type of the objects in the second case because in the both the cases the collection uses generic types.
D. Ogranos wrote:
They enable the compiler to spot obvious mistakes like inserting integers into string lists. But on the byte code level there isn't a difference between a generic and non-generic list.
Arun.V.Somasundaram
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |