wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Method Infer Type Parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Method Infer Type Parameter" Watch "Generic Method Infer Type Parameter" New topic
Author

Generic Method Infer Type Parameter

Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
Posts: 53
Given the following method is declared in class Utilities:

static <E, K extends E> boolean containsV3(K key, E[] array) {
for(E element : array)
if(key.equals(element)) return true;
return false;
}

Why do these calls result in errors?:

assert Utilities.containsV3("Hi", intArray) == false; // (10) Compile-time error!
assert Utilities.containsV3(30.5, intArray) == false; // (11) Compile-time error!

Can't E be inferred to be Object in 1st call and Number in 2nd call?
Ankit nShah
Greenhorn

Joined: Jul 01, 2011
Posts: 5
hi jeet,

static <E, K extends E> boolean containsV3(K key, E[] array) {

declaration means that containsV3 accepts two arguments one is array of E type and other is K which should be sub type of E.

In your first call
assert Utilities.containsV3("Hi", intArray) == false; // (10) Compile-time error!
E seems to be int[] and K is String so K subtype of E does not hold true and hence the error

Similarily for second
assert Utilities.containsV3(30.5, intArray) == false; // (11) Compile-time error!
E seems to be int[] and K is autoboxed to Double so K is also not subtype of E

One working eg for this could be
Number[] d = {1.2 ,3.4};
assert Cloning.containsV3(1.2, d) == true;
Here E is Number and K is Double subtype of Number.
Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
Posts: 53
thanks for your reply sorry i should have mentioned it that intArray is Integer[]

But i dont get why cant E be inferred to be Object so then String is a subtype of Object and Integer[] is a subtype of Object[] in the 1st call and the call succeeds? Similarly why cant E be inferred to be Number in the 2nd call so that Double is subtype of Number and Integer[] is subtype of Number[] and this call too succeeds?
Ankit nShah
Greenhorn

Joined: Jul 01, 2011
Posts: 5
Hi,

Since you are passing Integer[], E is taken as Integer. and K should be a subtype of Integer only.

But i dont get why cant E be inferred to be Object

If this would happen then since all object inherits from Object, E will always be Object and there is no meaning in providing types.
Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
Posts: 53
Then even this should give an error as E can always be Object:

static <E> boolean containsV2(E key, E[] array) { // (2) Generic version
for(E element : array)
if(key.equals(element)) return true;
return false;
}


assert Utilities.containsV2("Hi", intArray) == false;
Ankit nShah
Greenhorn

Joined: Jul 01, 2011
Posts: 5
Sorry about not framing it properly.
What I meant was that if E was inferred as Object, then every other object would be a subtype of E and so your K which is subtype of E wont have any meaning.
Jeet Jain
Ranch Hand

Joined: Sep 01, 2011
Posts: 53
ohhh ok. Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Method Infer Type Parameter
 
Similar Threads
Assertions
is this a proper mock question for the exam?
Unreachable statements
my notes on JLS for any1 who needs them !!
Simple J2SE 5.0 Tiger Notes