wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Question from K&B 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Question from K&B" Watch "Generic Question from K&B" New topic
Author

Generic Question from K&B

Amit Wadhwaa
Ranch Hand

Joined: Feb 15, 2007
Posts: 74
16. Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:
// INSERT DECLARATIONS HERE
output = process(input);
Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow
the code to compile? (Choose all that apply.)

-- The errata changes the qs to List<E> instead of <? super E> but can somebody tell me the answer with explanation of the unmodified question


SCJP 5 94%<br /><a href="http://amit-wadhwa.blogspot.com/" target="_blank" rel="nofollow">My Blog</a>
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Amit,

Good question...

Consider,
You asked a supplier a Box of SCJP 5.0.

Can you obey: if supplier says I will send a box of SCJP books but it may contain 1.1 books or 1.2 or 1.3 or 1.4 or 5.0.

The thing is you can't assign ambiguity to some specific thing because of TYPE ERASURE.

But you can do the reverse. because compiler can check that. like

List<? super SCJP5.0> bookBox= new ArrayList<SCJP1.1>(10);

You can't assign like this:

List<SCJP5.0> bookBox = new ArrayList<? super SCJP5.0>(10);
/** <? super SCJP5.0>(10); is not compile time constant;
To check at runtime TYPE ERASURE will happen;
So we can't do this;
**/

Note: SCJP5.0 extends SCJP 1.4 extends 1.3 extends 1.2 extends 1.1


Lets Program Better


[ March 24, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Amit Wadhwaa
Ranch Hand

Joined: Feb 15, 2007
Posts: 74
Srinivasan, I dont understand the whole explanation, It seems to me you are saying it is not allowed.. that was what I first thought but check out the following program:

package com.scjp.collections;

import java.util.ArrayList;
import java.util.List;

public class X{

public static void main(String[] args) {
ArrayList<Number> input = new ArrayList<Number>(10);
List<? super Integer> output = process(input);
output.add(5);
output.add(4);
System.out.println("Size is : "+output.size());
for(Object i: output)
System.out.println(">"+i+"<");
}
public static <E extends Number> List<? super E> process(List<E> nums) {

return nums;

}
}



This gives the output:
Size is : 2
>5<
>4<


Here the following assignment worked:
List<? super Integer> output = process(input);

That's why I posed the question what is the exact criteria for such assignment i.e. what other declarations can I have on the left side of "=" in the above statement
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Amit,

What i said was,
"The thing is you can't assign ambiguity to some specific thing because of TYPE ERASURE.".

what you told though example:
"I will make both return type and receiving type match, then it will allow"

Both are correct.

No confusion for you right!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Question from K&B
 
Similar Threads
doubt in generics example from kathy sierra
Generics Question from K&B Study Guide.
Question of K&B book on Generic Collections
Study guide 5 Self Test error
please explain this!!