*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Question" Watch "Generics Question" New topic
Author

Generics Question

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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


SCJP6.0,My blog Ranchers from Delhi
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1013
    
  15
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
Bartender

Joined: Sep 06, 2012
Posts: 1013
    
  15
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

Joined: Mar 17, 2010
Posts: 1051

Thanks for your reply but where actually we are using this.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1013
    
  15
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

Joined: Mar 17, 2010
Posts: 1051

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
Sheriff

Joined: Sep 28, 2004
Posts: 18750
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Generics Question