• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

 
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
please tell me why it is giving Error:




BUT IN THIS CASE WHY IT IS NOT GIVING ANY ERROR:

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this and this. I expect this discussion to be moved.

If you have a lower wildcard bound, how do you know the number will be an integer? If you have the upper wildcard bound, you know it will be a Number or an Object, so you can box the number to an Object (runtime type Integer).
Google for Angelika Langer Java generics FAQ and look for the two generics sections in the Java Tutorials.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Prakash Rai: In the future, please TellTheDetails(←click). In particular, in a case like this, copy/paste the exact, complete error message. That makes it easier for people to understand what the problem is.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Angelika Langer Java generics FAQ.


That's a sweet plum, there Campbell. Very impressive. I'm always impressed with THOSE people (who can document well).
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Angelika Langer’s site is worth searching.
 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been studying up on Generics (via Oracle's tutorial) and I'm in the same boat as Prakash.

I took his example one step further by replacing "99" with an Integer:



-- and it won't compile:

GenericsTest.java:8: cannot find symbol
symbol : method add(java.lang.Integer)
location: interface java.util.List<capture#240 of ? extends java.lang.Number>
list.add(new Integer(99));

I'm at a loss. If the answer is on Angelika's FAQ or the Oracle tutorials...I can't find it.



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have this:


So all the compiler knows is that it's List<Number> OR List<something that extends Number>. It could be List<Number>, or it could be List<Integer> or it could be List<Double>, etc. When determining what kind of List it is when we go to use it, that's all the compiler knows and cares about. It doesn't look at the = new ArrayList<Number>() part.

So then, when we call add(Integer), the compiler has to stop us, because it doesn't know if we're trying to add that Integer to List<Number> or List<Integer>, which would both be fine, or to a List<Double>, which would not be fine. It can't be sure that it's a valid use of that list, so it's an error.
 
Marshal
Posts: 28193
95
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
You're in the same boat as a lot of people. This:



means that you're declaring a List of some class X which extends Number. Since X might not be Integer, you can't add an Integer to that List.

You're probably in the same boat as all those other people who thought that was declaring a List which could contain any objects of classes which extend Number. I haven't looked at Langer's magnum opus for several years but I'll bet it does explain that basic distinction.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that List<? extends Number> means that you have a list of some unknown type that extends Number.

You can't add anything to such a list, because the compiler doesn't know, just by looking at the type of the list, what the unknown type is. Suppose you create a list like this:

You should only be able to put Integer objects into this list, because on the right side you created it as an ArrayList<Integer>. You'd expect to get a compiler error, or at least an exception (maybe ClassCastException), if you'd try to put another object in it that extends Number:

However, at the point where you try to add something to this list, the compiler only knows the type of the variable, List<? extends Number>. From that, it cannot know that only Integer objects are allowed in this list.

And because of type erasure, it's also impossible to check at runtime if you're trying to put something into the list that isn't supposed to go in there.

So, to make it safe, Java simply disallows adding anything (except null) into such a list.

Type erasure: Generic type parameters are discarded by the compiler. So at runtime, a List<? extends Number> and an ArrayList<Integer> look like a plain old List and ArrayList without generics.

One of the places where this is described in Angelika Langer's FAQ is here: Which methods and fields are accessible/inaccessible through a reference variable of a wildcard parameterized type?
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holy smokes. Just when I thought I grasped the concept of generics, the page is turned.

Thank you Jesper, Paul, and Jeff!
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:It doesn't look at the = new ArrayList<Number>() part.


You mean it doesn't look "back" after the assignment/declaration is over, Jeff? It seems incomprehensible that an assignment during declaration is not checked.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Witten wrote:

Jeff Verdegan wrote:It doesn't look at the = new ArrayList<Number>() part.


You mean it doesn't look "back" after the assignment/declaration is over, Jeff? It seems incomprehensible that an assignment during declaration is not checked.



Maybe. I meant exactly what I said.

When determining what kind of List it is when we go to use it, that's all the compiler knows and cares about. It doesn't look at the = new ArrayList<Number>() part.



Maybe the two underlined parts being in different sentences obscured the fact that they're part of the same overall assertion.

As for "looking back," I'm not a compiler guy, so I don't know the order in which various analyses and validations are done, and as far as the point I was making, it's irrelevant. Yes, it has to check that assignment, but that is completely independent of anything else we do with that list.
 
Prakash Rai
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Paul Witten wrote:

Jeff Verdegan wrote:It doesn't look at the = new ArrayList<Number>() part.


You mean it doesn't look "back" after the assignment/declaration is over, Jeff? It seems incomprehensible that an assignment during declaration is not checked.



Maybe. I meant exactly what I said.

When determining what kind of List it is when we go to use it, that's all the compiler knows and cares about. It doesn't look at the = new ArrayList<Number>() part.



Maybe the two underlined parts being in different sentences obscured the fact that they're part of the same overall assertion.

As for "looking back," I'm not a compiler guy, so I don't know the order in which various analyses and validations are done, and as far as the point I was making, it's irrelevant. Yes, it has to check that assignment, but that is completely independent of anything else we do with that list.



I Understand Something but I am very much very sure about this...I have gone through suggested posts by all of you but even I need bit clear solution. What Should I do in order to solve that?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prakash Rai wrote:
I Understand Something but I am very much very sure about this...I have gone through suggested posts by all of you but even I need bit clear solution. What Should I do in order to solve that?



Are you asking about the post you quoted? If so, I'm not sure what you're trying to solve.

Or are you asking about your original question? If so, then the simplest answer is That is, get rid of the ? extends part. Without knowing more about what you're actually trying to accomplish, though, it's hard to say if that's an appropriate design for the problem at hand.
 
reply
    Bookmark Topic Watch Topic
  • New Topic