File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics " Watch "Generics " New topic
Author

Generics

Prakash Rai
Ranch Hand

Joined: Jan 10, 2011
Posts: 102

Hi
please tell me why it is giving Error:




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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

@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.
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
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
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Yes, Angelika Langer’s site is worth searching.
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 150

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 150

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

Joined: Oct 10, 2012
Posts: 86
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 10, 2011
Posts: 102

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics
 
Similar Threads
Quick ArrayList Question
List example
Not under standing Generic Parameterised Types
Adding elements into generic list doubt
sum of non numeric elements in an ArrayList