Java Generics - What's really in a unbounded wildcard?

后端 未结 2 1546

If i have the following code :

public static void main(String [] args) {  
        List  l2 = new ArrayList ();  
        List &l         


        
相关标签:
2条回答
  • 2021-01-23 06:27

    The unbounded wildcard is reifiable only in that no type information is lost at runtime because there's no type information to lose. As to your questions:

    a. The test method doesn't know that l2 has Integer type. To the test method, it's a List containing "something", and that's all.

    b. There's no "translation". It's just a list of unbounded type passed as a parameter to a method that takes a list of unbounded type.

    0 讨论(0)
  • 2021-01-23 06:47

    I don't believe the <?> is reified. It is simply the only way to refer to a generified type without using the raw form (List). In both cases you are simply doing the exact same operation as:

    if (l instanceof List) 
       ...
    

    Edit

    Indeed I have just verified that they generate absolutely identical bytecode whether you use List<?> or List in the instanceof.

    0 讨论(0)
提交回复
热议问题