File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Question

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What wrong with this .

Even though Integer extends Number.

List<? extends Number> lst2 = new ArrayList<Integer>();
Integer num2 = new Integer(20);
lst2.add(num2); // Line 3

But still we are getting error at Line No 3.Could not understand why,Can anybody help me with this.

Thanks
Shanky
 
Chan Ag
Rancher
Posts: 1089
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you declare a type with the wildcard <? extends SomeClass>, after initialization you cannot add new elements to the list.

But you can do this -




Chan.
 
Chan Ag
Rancher
Posts: 1089
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this is because lst2 could be an ArrayList<Number>, ArrayList<Integer>, ArrayList<Byte> .... and so on. Compiler does not know.

What if it was an ArrayList<Byte> and you added an Integer into it by mistake. Compiler has no means to ascertain that such an operation should result in a compiler error. Therefore if compiler allows the above, it cannot allow us to add new elements to the collection. Removal should be no issue.

Chan.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply but where actually we are using this.
 
Chan Ag
Rancher
Posts: 1089
14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First I don't get what kind of a Thanks that is. Anyway I will ignore that.

But still we are getting error at Line No 3.Could not understand why,Can anybody help me with this.


I thought that was your question. Or was it not. My response addresses the above question.

Now I see you have a second question. That seems alright.

Thanks for your reply but where actually we are using this


Using what?

Your code? You wrote it, remember?


 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not quoting my complete question.
I always did such stupid mistakes in hurry.

what i actually mean is why do we have
List<? extends T>
Where we are actually using this in a real time sceneria.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:
what i actually mean is why do we have
List<? extends T>
Where we are actually using this in a real time sceneria.


How about a method to add up all numbers from the list? And you don't care if the caller passes a List<Integer>, List<Float>, or List<Double>? By having your parameter be of type List<? extends Number>, you can take any of the three list types and calculate the sum.

Henry
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Shanky Sohar wrote:
what i actually mean is why do we have
List<? extends T>
Where we are actually using this in a real time sceneria.


How about a method to add up all numbers from the list? And you don't care if the caller passes a List<Integer>, List<Float>, or List<Double>? By having your parameter be of type List<? extends Number>, you can take any of the three list types and calculate the sum.

Henry


Great henry,
I was just looking up the src file of HashMap,Arraylist etc to understand this throughly.

Thanks a lot for clearing this.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic