Java Generics: dropping a raw type to any type that is retyped does not generate a warning without warning

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
   }

}

and. Point 1, of course, will not give an immediate warning warning
b. In paragraph 2, there were also no warnings of immediate warning.

I noticed that casting from a raw type (obj) to an ANY reifiable type (e.g. GenericBridgeMethods or GenericBridgeMethods <?>) Will not give an immediate growth warning. If you run this code, a runtime error will execute in section 2.

Should the compiler give a warning in section 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 someone explain why only condition 4 has an error?

+5
1

, GenericBridgeMethods, , T . Reifiable , . T.

2 , : , obj GenericBridgeMethods. , T .

, , - :

GenericBridgeMethods<String> g1 = (GenericBridgeMethods<String>) obj;

, , obj GenericBridgeMethods String, . , , :

List<String l1 = (List<String>) obj;

, List GenericBridgeMethods, , GenericBridgeMethods . GenericBridgeMethods, List, .

, GenericBridgeMethods (, , ). .

, , :

public static void main(String[] args) {
   List obj = new ArrayList<Integer>();

   //this is allowed (no warning), even though it will fail at runtime
   CharSequence sequence = (CharSequence) obj; 
}

obj CharSequence, , . , , obj List. List , CharSequence, List, .

, . , .

- "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

, " 4" . , , .

ArrayList a1 = new ArrayList<Integer>(); // clause 3
Number n1 = (Number)a1; // clause 4 ERROR

a1 Number , Number ArrayList - , . Java , Number, ArrayList, Number ArrayList . , , .

ArrayList a1 = new ArrayList<Integer>(); // clause 3
Comparable c1 = (Comparable)a1; // clause 5

Comparable , ArrayList Comparable.

List l1 = new ArrayList<Integer>(); // clause 6
Number n2 = (Number)l1; // clause 7

List , Number List. , , l1 ArrayList.

+11

All Articles