• 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

unexpected compilation error with Generics

 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the practice exam for Generics on
NikoJava

Question Number 30

I would expect the following to compile:


Compilation in fact returns:

cannot find symbol
symbol : method add(java.lang.Object)
location: interface java.util.NavigableSet<capture#640 of ?>
set.add(new Object());
....^

why does it not compile and why is it complaining about the add method?

I read that Collection<?> is different from Collection<Object> in that <?> makes it homogenous and <Object> makes it heterogenous, but why would that exclude me from adding Objects, why would it complain about the add method, and why does it say capture#640 of ?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler only looks at the reference type, which in this case is NavigableSet<?>. You won't be able to add anything through a NavigableSet<?> reference, because the wildcard means that the actual object pointed to might be of any generic type. For example, it could be a TreeSet<Integer>. If you understand why you can't add an Object to a TreeSet<Integer>, then you understand what is going on.

The only generic collection references you can add elements through are those references which establish a lower bound for the type of the contained elements. You can do that by simply providing a specific type, as in NavigableSet<String>, in which case you can add String and any subtype of String. Or you can do it with the <? super X> syntax: If you have a NavigableSet<? super Integer> reference, you can add Integer or any subtype of Integer to it.

But these type of references don't work because they don't establish a lower bound for the generic type:

NavigableSet<?> // Unbounded type (except for the absolute upper bound, which always is Object)
NavigableSet<? extends Integer> // Upper bound, but no lower bound
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that actually helps a lot. Thanks Ruben.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

Glad that helped.
 
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic