• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Generic not compiling

 
Ranch Hand
Posts: 106
Hibernate Python MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why doesn't
compile?

What would be the logical explanation?

Thanks
Karsten
 
Sheriff
Posts: 9709
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
the compiler doesn't know the exact type of a collection when you use <? super Type> syntax. What the compiler knows is that you can add elements of Type to that collection but not its super classes. In your case you can add elements of type String to the set but not super classes of String...
 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate Python MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok this makes sense: the only safe operation would be to add a String, because the collection could be anything of a higher type. Since an Object isn't a String, the operation is considered to be unsafe -> error.

Thanks for helping!
Karsten
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:the compiler doesn't know the exact type of a collection when you use <? super Type> syntax. What the compiler knows is that you can add elements of Type to that collection but not its super classes. In your case you can add elements of type String to the set but not super classes of String...



line 1 if uncommented gives error.
<? super Type> says you can add elements of Type to that collection but not its super classes,also please accept any collection with generic type or a it's super type.
here you can pass super type to the method demanding <? super Type> but can't add super type to collection directly.
 
reply
    Bookmark Topic Watch Topic
  • New Topic