This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with generics  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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: 23506
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot for the help. It is clear now.
 
We don't have time for this. We've gotta save the moon! Or check this out:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!