I have the following question regarding the code below:
public class GenericBridgeMethods <T> {
public static void main(String[] args) {
List obj = new ArrayList<Integer>();
List <?> l1 = (List<?>) obj; // clause 1
GenericBridgeMethods <?> g1 = (GenericBridgeMethods<?>) obj; // clause 2
}
}
a. Clause 1 of course won’t give an unchecked cast warning
b. Clause 2 also did not give an unchecked cast warning
I noticed that a cast from a raw type (obj) to a ANY reifiable type (like GenericBridgeMethods or GenericBridgeMethods <?>) will not give a unchecked cast warning. If you run this code, a runtime error will occur at clause 2.
Shouldn’t the compiler give a warning at clause 2
EDIT 1:
ArrayList a1 = new ArrayList<Integer>(); // clause 3
Number n1 = (Number)a1; // clause 4 ERROR
Comparable c1 = (Comparable)a1; // clause 5
List l1 = new ArrayList<Integer>(); // clause 6
Number n2 = (Number)l1; // clause 7
Comparable c2 = (Comparable)l1; // clause 8
Can anyone explain why only clause 4 has error?
Well, first off in
GenericBridgeMethodsas you have defined it,Tis not a reifiable type. Reifiable means that the type will be encoded into the class and will be available at runtime. That is not true ofT.Clause 2 does not give a runtime warning because it is checked: There will be a runtime check that
objis type-assignable to theGenericBridgeMethodstype. Since you’ve opted for a wildcard as the type parameter, nothing aboutTneeds to be checked.If on the other hand you did something like this:
that would give you an unchecked assignment warning because the fact that
objis aGenericBridgeMethodsofStrings cannot be checked at runtime. However, the same warning would appear if you had done this:Edit
If you’re confused as to why the compiler allows you to try to cast a
Listto aGenericBridgeMethods, the answer is because the compiler can’t know the entire hierarchy ofGenericBridgeMethodsand its subclasses. There could be a subclass ofGenericBridgeMethodsthat implementsList, in which case the cast might be legitimate.You will however get a compile error if you made
GenericBridgeMethodsa final class (and thus prevented it from having subclasses). In this case, you will get an inconvertable types error.Just to show you your question has little to do with reifiable types and generics, take a look at this:
You can explicitly cast
objto aCharSequenceeven though you know that it will fail at runtime. The reason is because all the compiler knows is thatobjis a type ofList. SinceListis an interface, there could be an implementation ofCharSequencethat is also aList, and so the cast must be permitted.Every explicit cast carries a degree of possibility that it could fail at runtime. Otherwise, it would be a redundant cast and the compiler should allow you to omit the explicit cast.
Edit – Regarding your “edit #1”
You are wondering why only “clause 4” does not compile. I think I explained this already above and in the comments, but I’ll go thsough this specific example for you step-by-step.
Casting
a1toNumberdoes not work becauseNumberandArrayListare both classes, not interfaces. Because Java does not allow inheritance from multiple classes, for an object to be an instance of bothNumberandArrayList,Numberwould have to be a subclass ofArrayListor vice versa. This is known to not be true at compile time.Since
Comparableis an interface, a subclass ofArrayListmight be aComparable.Since
Listis an interface a subclass ofNumbercould implementList. The compiler does not know when checking the cast thatl1holds anArrayList.