wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic return type 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 "Generic return type" Watch "Generic return type" New topic
Author

Generic return type

Piotr Trochim
Ranch Hand

Joined: Dec 29, 2006
Posts: 35
Howdy y'all!

Let me get straight to the point:



Tried to compile on java 1.5.11 and got the following error message:


Type mismatch: cannot convert from List<capture-of ? super Number> to List<Number>


But wait - the fun's only about to begin

Change the types of both output and input reference variables to List<Number> - You'll get the same stuff.
Change output to List<Number> and input to List<Integer> - same stuff.

TYhe problem is in the return type.

Let's analyse that:
1.) E is any type that passes IS-A test for being a Number.
Both Number and Integer play along, so if we're either passing a list of Numbers or a list of Integers as a method's param - it's OK.

2.) The return type is something that is a supertype to the type we passed.

So if I passed a list of Integers, I would expect to be able to assign the returned reference to the reference types: List<Object>, List<Number> or List <Integer>.
If I was to pass a list of Numbers, I would expect the compiler would let me assign the returned value to the reference types: List<Object> and List<Number>.


But instead - I get that weird looking error message. I checked the same with 'extends' frame - same thing.

Does it mean that I can't specify 'fuzzy' generic return types - only the solid ones?

I'm looking forth to Your feedback.

Cheers,
Paksas
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
you have templated the method to be <E extends Number>

but with the return type you are saying that you can return anything that is super of E so you could for example return List<Object>.
Object does not extend Number that is the problem.


Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Let's consider carefully the method signature:


As you said before E could be a Number or anything that extends Number. Some examples:

1) If E is an Integer, your method signature will resolve to:

The return type can be any List whose element extends Integer; that would be List<Integer>, List<Number> and List<Object>.
Now, imagine the following:

or even:


Because a List of Objects is not the same as a List of Integers (even if an Integer is an Object).

- If E is Number, then your method signature will resolve to:

Here, again, the following won't work:

or even:

The same logic applies: a List of Numbers is not a List of Objects. (Think about this: a List of Objects can hold Strings as well as Numbers and any object of any class out there; however, a List of Numbers can only hold Numbers and its subtypes.)

I hope those examples made it a little clearer for you.

[Edit]
Your example could be corrected in this way:

[ June 10, 2007: Message edited by: Sergio Tridente ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic return type