2

If i have the following code :

public static void main(String [] args) {  
        List <Integer> l2 = new ArrayList <Integer>();  
        List <?> l3 = l2;  
        test(l2);  
        test(l3);  
}  


public static void test(List <?> l) {  
        if (l instanceof List<?>)  
            System.out.println("true");  
}  

This will print:

true  
true  

From what i understand, <?> is a reifiable type, which means it has some capture type (whatever that type is) which is available at run time.

Questions:
a. In test method, does it know that l2 has integer type (since it has been erased prior to the method call)? How does it translate so that l (from l2) is instanceof List <?>?
b. What about l3? How does it translate that?

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
yapkm01
  • 3,590
  • 7
  • 37
  • 62
  • 1
    java does not have reifiable types, so the test method knows nothing, and instanceof will never know anything about the generic types. – jtahlborn Oct 17 '11 at 15:50
  • 1
    @jtahlborn: (and voter) The [JLS disagrees with you](http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.7). – Ryan Stewart Oct 17 '11 at 16:38
  • k, was thinking more about how it applies in the general sense to generics. i see now that it also refers to the standard types (which are reifiable). the rest of my statement holds though. – jtahlborn Oct 17 '11 at 17:58

2 Answers2

8

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.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • 1
    But > is a reifiable type. It's the only reifiable type for wildcard .. you can check lots of SCJP books. Here's one of the link http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#FAQ106 – yapkm01 Oct 17 '11 at 16:14
  • 1
    @yap: In that article it's saying that `>` is reifiable in the sense that after erasure, no information has been lost about the type of the list (because nothing is known about the type parameter to begin with!). But it's not reifiable in the sense that that information is actually available for an object at runtime--at runtime, you don't *know* that it was a `List>` rather than a `List` or a `List`. – Mark Peters Oct 17 '11 at 16:34
  • The answer provided by Ryan Stewart is more accurate than the answer proided by Mark Peters. Please correct me if i am wrong:-) – Rekha Nov 23 '11 at 05:57
3

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.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199