Originally posted by Pawan preet:
Can anybody SCJP 5.0 guy can correct me if my last two replies on Generics are incorrect.
They are correct. When there is cast, exception will be thrown. Question is when compiler cast.
Here I am taking three different cases for object retrieval from the list and along with I am also showing you the output from javap -c GetReturn
Case 1:
Case 2:
Case 3:
In all the above cases, object returned from the list is assigned to Object not Integer but there is a cast operation only in the third case done by compiler. (i.e., in-enhanced for-loop)
So now its clear that only enhanced for loop will return ClassCastException because of implicit cast in that case even if you assign it in Object.
Naseem
[ August 08, 2006: Message edited by: Naseem Khan ]