• 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:

help with generics

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can you please help me understand why the below code compiles fine



and the below one does not?



Regards
Deepa
 
Ranch Hand
Posts: 37
Oracle Tomcat Server Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepa Sobin wrote:Hi,

Can you please help me understand why the below code compiles fine



and the below one does not?



Regards
Deepa


You can't add to collections with generic types of <? extends Something>. Otherwise you could do something like this:
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 2, the compiler doesn't see that you have instantiated an ArrayList<Object>. It only sees the declared type.

In the first example, the compiler sees <? super Object> and knows that whatever type the List holds, Object IS-A whatever type it may happen to be. So it is ok to add.

But in the second example, the compiler sees <? extends Object>; the List is some type that extends Object; it could be Object, but it could be some other type, and it might not be true that Object IS-A that type. So adding is not allowed.
 
Deepa Sobin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case, why does the below code fail to add a Number into this list which can hold any super type of Integer?

 
Eli Wood
Ranch Hand
Posts: 37
Oracle Tomcat Server Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepa Sobin wrote:In that case, why does the below code fail to add a Number into this list which can hold any super type of Integer?


You use ? when you don't know what the specific type of the objects in the collection will be, but in this case you know the array either holds Integer objects or objects of some superclass of Integer.

Remember, the compiler only looks at the reference type of intList, not the object type. Two possibilities are that it could be an ArrayList<Number>, or perhaps an ArrayList<Integer>. As far as the compiler knows, it could be either one. So it only makes sense for the compiler to allow you to add elements which pass the IS-A test for the type declared in List<? super type>, since those objects would definitely also pass the IS-A test for any superclass of type.

Effectively that means you can only add Integer objects, or any objects of any subclass of Integer, to that List. It just so happens that Integer has no subclasses, so only Integer objects may be added.
 
Sheriff
Posts: 28401
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepa Sobin wrote:In that case, why does the below code fail to add a Number into this list which can hold any super type of Integer?



That is not a correct description of what "List<? super Integer>" means. It does not mean "A list of objects whose class is any superclass of Integer". It means "A list of objects of type X, where X is some unknown superclass of Integer".

So a List<? super Integer> variable might refer to a List<Integer> or to a List<Number> or to a List<Object>. Since it might be a List<Integer>, the compiler cannot permit a Number to be added to it, because that Number might not be an Integer.
 
Deepa Sobin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the help. It is clear now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic