• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Generic compilation issue

 
Greenhorn
Posts: 7
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can anyone please help me to understand this code? As per my understanding below code should take Object() as argument but it gives compile error. Thanks

GenericTest.java:5: cannot find symbol
symbol : method add(java.lang.Integer)
location: interface java.util.NavigableSet<capture#964 of ?>
set.add(new Integer(2));
^
1 error

 
Bartender
Posts: 2447
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The generic set with <?> won't let you add anything except null as a rule of the compiler to make your collection type safe.

During compilation time, the set means it refers to a collection that contains a type of object as specified on the right hand side.
During compilation time, the compiler does not know what set will contain. It can be Object, Animal, Cat, Dog....
For example, you may want to add this to the class in the future:

Again during compilation time, set.add( ...) will not compile because the compiler does not know what set will contain. The compiler won't let you put a wrong type of object to set.
 
chirag visavadia
Greenhorn
Posts: 7
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you now I understand...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic