• 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

Need generic clarification

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
K & B book for 310-055 i.e. java 5.
Chapter 7: Generics and collections,
Self test example 16.
Given the method declared as :

A programmer want to use this method like this:

Which pair of declarations could be placed at //INSERT DECLARATIONS HERE to allow the code to compile?
Answeres are as bellow according to book.
A. ArrayList<Integer> input = null;
List<Integer> output = null;
E.List<Number> input = null;
List<Number> output = null;
F.List<Integer> input = null;
List<Integer> output = null;

Actually in netbeans with java 1.6. any of the above does not compile and gives error as
Compiling 1 source file to /home/sharmila/First/build/classes
/home/sharmila/First/src/generics/Test.java:98: incompatible types
found : java.util.List<capture#17 of ? super java.lang.Integer>
required: java.util.List<java.lang.Integer>
output = process(input);
1 error
BUILD FAILED (total time: 0 seconds)
Is the problem with my interpretation or there is some error in book?
Please help for this. I am new to generics and have littlebit problems in interpreting generics.
Sharmila
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, of course all the three correct answers (!) are incorrect, here is why (It’s might look exhaustive but necessary):
Whenever we use wildcard in our codes, compiler uses a technique known as “Wildcard Capture”. It basically is a way for compiler to deal with wildcard means that internally compiler converts the wildcard types to some anonymous type like this:
List<?> becomes List<capture#000 of ?>
List<? extends Number> becomes List<capture#000 of ? extends java.lang.Number>
List<? super Number> becomes List<capture#000 of ? super java.lang.Number>
And so on...
So now what is the capture of a wildcard compatible to? Means what kind of type a the capture of a wildcard can assign to?
Well, the answer is another wildcard! But never, ever a concrete type! So do you get the point?

The return value of the method is: List <? super E> and you never can assign it to a concrete type like List<Integer> and this is the case in the whole 3 method call which all are wrong! To fix the method call you should write:

<E extends Number> List<? super E> process(List<E> nums)

when you use the List<Integer> as the input the compiler infer the type E as Integer so the output should be compatible to <? super Integer> which is again <? super Integer>, so it should be:

ArrayList<Integer> inputA = null;
List<? super Integer> outputA = null;
outputA = DumpArray.process(inputA);

or in the second method call it should be:
List<Number> inputB = null;
List<? super Integer> outputB = null;
Or
List<? super Number> outputB = null;
outputB = process(inputB);

One last important point:
The wildcard capture is never compatible even to its bounds even if the bound is a final class!
Like this:
List<? extends String> lst1 = null
List<String> lst = lst2; //Compile error!

And unbounded wildcard capture (<?>) is compatible to nothing except itself!

That’s it! I have a K&B of SCJP 6 and could not find this question, Are you sure that they mentioned those as correct answer?!!
 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Morteza,
Very nice explaination.
I have K & B of SCJP 5 NOT 6.
Any way rest of the book is good with good explaination on each subject.
I love the way K & B explain the topics. Rest of the book is good.
Thanks again
 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem. Anyway if that's what the book says, then I guess the authors need some sort of Generics Clarification too!
 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Moretaza,
It was very good and technical and appropriate explaination
Thanks again
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic