• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics/wildcards in variable declaration

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I'm trying to nail down the rules regarding generics. I have the following class/test cases:



On the line marked "ERROR" I get this from the eclipse workspace:
The method meth(capture#2-of ? extends Number) in the type
Test1<capture#2-of ? extends Number> is not applicable for
the arguments (float)

I would have guessed that because Test1 was parameterized with <Float> that T would resolve to Float. It appears that at this point, the only valid T argument for meth is null. Someone please explain.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would have guessed that because Test1 was parameterized with <Float> that T would resolve to Float. It appears that at this point, the only valid T argument for meth is null. Someone please explain.



By the time you call the method, T has already been resolved. It uses the reference for this -- and you told the compiler to treat T as "? extends Number", which means an unknown type that extends Number. And since the compiler don't know what type it is, it won't allow you to add anything that many not be that type.

Henry
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Float is a subtype of Number, just as Object is a super type of String in the declaration before it and Number is a super type of Integer in the declaration after it. Why the change in behavior for the extends wildcard declaration?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian, when you say <? super String>, then you are sure that its a super type of String. So whatever ? is, it is higher than String in the class Hierarchy. So it will definitely be able to fit a String into it. I think you understand this as it is simple polymorphism. Eg



But when you say <? extends Number>, then you are not sure what ? exactly is. You are only sure that its a sub-type of Number. It could be Integer, or Float, or anything. So how can you add an Integer or Float to it. Suppose you add an Integer to it but it actually is a list of Float. Then this would be disastrous. So when you use the extends word with ?, then you are not allowed to add any elements. But one thing's for sure, and that is that the elements will fit into Number as the actual type would be a sub-type of Number (or Number itself). So you can get elements from that list into Number references



With the super syntax, you do not have this liberty. When you say <? super String> then ? could be anything. So you can only get elements from that list into references of type Object as it is the base type of all the types so you are sure, whatever objects are in the list, they will fit into Object type

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, Ankit - But there is no add in meth! And it fact I don't see mention of Collection anywhere in the code... ???
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would have guessed that because Test1 was parameterized with <Float> that T would resolve to Float. It appears that at this point, the only valid T argument for meth is null. Someone please explain.



Since 'new' is processed only at runtime, compiler doesn't get to know the type of object. Compiler just relies on type of reference variable - which in this case contains wildcard.

Compiler won't allow any operation from within the object if its type is wildcard. However compiler does allow some operations such as assigning the object to another reference variable (if it meets the wildcard specification).
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I think it's clearer now.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic