Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Tough Generics question from S&B 5.0 book  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted this a couple of days ago but didn't get any responses. I've tried to make this explaination a bit clearer in case it was difficult to follow before.

Here is the question as presented in chapter 7 of the new SCJP book by Sierra and Bates



I have two problems with the answer. First, I don't understand why C is wrong. My take on <? super E> is that the returned List can by of any super type of E. Since E would be <Integer> then <Number> should work.

My second problem is that in an effort to play with these constructs I made a little program. I can't even get the "correct" versions to work.

Here is the code



Cases A and F give this compiler error


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



Case C gives this compiler error


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



Case E gives this compiler error


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




So as near as I can tell, all of these fail for essentially the same reason but I don't know WHY?

Anyone kind enough to point out where I've run amuck will be most appreciated.

Thanks
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a very interesting question!
you should contact directly Bert Bates, but I think he's deservedly going into hibernation after the SJCP5 Study Guide publication

B.
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using JDK 1.5.0_02, this is the compiler error for option C.

Test.java:12: <E>process(java.util.List<E> in Test cannot be applied to (java.util.ArrayList<Integer>
output = process(input);
^
1 error
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static <E extends Number> List<? super E> process(List<E> list)

What's the meaning? My problem is to understando this (? super E).

What I understand is that List will need to have anything that extends from number, but and the meaning of (? super E) what will it be?

thank you.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As it happens, this is a question that I wrote. And somehow I managed to mangle it just before submitting it, and the error went undetected until now. The original, tested, correct version of this question used the following declaration for process():

Note the return type is List<E>, not List<? super E>. With this version, the answers given are correct, and the explanations should make more sense.

I'm not sure what happened here - I remember experimenting a bit with the form of the question, to see if there was a way the question could also involve using super. But I couldn't find a way to do this that made sense, so I decided to go back to the original version of the question. Except somehow I accidentally left behind the <? super E> from an alternate version of the question. Oops.

So anyway, just replace List<? super E> with List<E>, and the question works as it was originally intended. And we'll get this listed in the errata shortly.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, if we dont change the method signature, i.e. keep it as


Then, what version of List should be given to make it work. I tried, but I could not get anything to work.

As I understand since E extends Number, lets assume that E is Integer. So ? super E implies that output list should be List<Number> or List<Integer>, since ? super E implies that it should either be of type E or a super class of E.
Therefore,
List<Integer> input = null;
List<Number> output = null;

output = process(input);
should work
or even for
List<Integer> output = null;
should work.

But, it is not working

Why is this happening and how should it work?
Can anyone tell me if my understanding is correct or I am missing something?
[ January 30, 2006: Message edited by: Hamid Virani ]
 
Andrew Shumway
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Then, what version of List should be given to make it work. I tried, but I could not get anything to work.
[ January 30, 2006: Message edited by: Hamid Virani ][/qb]<hr></blockquote>

Using the Generics class above the following declarations work



Because Number both extends Number and is super of Number



Like the above I can see that the input list will work because Number extends Number. However I can't see List<? super Integer> qualifies when the process method will return List<? super Number> However, I don't get a compile error. I'm guessng that since all super classes of Number are also super classes of Integer it's ok.



This also works for the same reason as the first.


--andrew
[ February 02, 2006: Message edited by: Andrew Shumway ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with the aswere B I get

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

what about you??
 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I've checked all of your suggestions and have found them working.

Originally posted by Andrew Shumway:


Then, what version of List should be given to make it work. I tried, but I could not get anything to work.
[ January 30, 2006: Message edited by: Hamid Virani ]<hr></blockquote>

Using the Generics class above the following declarations work



Because Number both extends Number and is super of Number



Like the above I can see that the input list will work because Number extends Number. However I can't see List<? super Integer> qualifies when the process method will return List<? super Number> However, I don't get a compile error. I'm guessng that since all super classes of Number are also super classes of Integer it's ok.



This also works for the same reason as the first.


--andrew

[ February 02, 2006: Message edited by: Andrew Shumway ][/QB]

 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


by Jim Yingst

...correct version of this question used the following declaration for process():

public static <E extends Number> List<E> process(List<E> nums) {



FYI, I check my book (ISBN 0-07-225360-6) and it has the correct version.

Richard
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!