aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Generics question" Watch "Generics question" New topic
Author

Generics question

Lata Bagga
Ranch Hand

Joined: Jun 13, 2007
Posts: 35
Hello Friends,

I have doubt in the following code:



I am not able to understand:
1) why A option gets compiled, how the findLarger(T x, T y) accept 123 and "456" even though both are different, one is Integer and other is String.According to me both the arguments should be same.

2) Why option B is giving compiler error as: "Type mismatch:cannot convert from Number&Comparable<?> to int".

I think I am not able to get the correct concept of this statement "public <T extends Comparable> T findLarger(T x, T y)" . Can anyone clarify my doubt?

Regards,
Lata
Rohit Garg
Ranch Hand

Joined: Feb 05, 2008
Posts: 30
I think that the parameterised declaration T extends Comparable is letting you to declare a class T which inherits the properties of Comparable class.

The input arguements that you pass to the function, the return type is also the same.

Thats why the b option is getting screwed as the return type changes to either Double or Comparable. 'int' doesn't qualify for both these options.


Regards,
Rohit.
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
I think you also need to look at the return type as it and the arguments are both of type T.

In the case of A I think all arguments are considered to be compatible with Object. In the case of the 123 it is wrapped to an Integer which of course extends Object.

In the case of B the arguments extend Number but the return type is int and the method could return anything that extends Number.
Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89
I think option a and c is correct.

B is incorrect because you cannot pass two different type of parameter for T and D is incorrect you cannot cast Double wrapper to int.


"Control time instead of letting time control you."
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi,

Well this is really a very good question..a proper explanation to this will really clear your concept regarding wild card and type parameter in Generics:

let me try to explain how compiler use your code here..!!

This line has a quite complex meaning for compiler:

public <T extends Comparable> T findLarger(T x, T y) {

lets break it in different part:

< T extends Comparable >

As per i think, Compiler use this to accept only those parameter which implements Comparable.
for ex: if you try to call this method by passing:
Object and Object --> compiler will not accept, because Object doesn't implements Comparable,but if you pass:
Integer and Integer --> Compiler will accept.
Integer and String ---> Compiler will accept as both implements Comparable
Compiler treats both arguments differently..!!

This part has only this role.

return type T --> It can return only type T.

now lets consider your Problem option one by one:

Option 1)
A. Object x = t.findLarger(123, �456�);

now see
123 --> Integer --> implements Comparable
456 --> String ---> implements Comparable

Now how compiler can accept String and Integer ?

here compile do a conversion...it sees is there is something specific to both..
for eg:
Integer extends Object
String extends Object

So compiler see both as Object and accept them..so from compiler point of view:
T x ---> Object x --> Object x = new Integer(123)
T y ---> Object y --> Object y = new String(456);

and we can call Object.compareTo(Object)

again return type is int --> Integer --> extends Object
so this works fine.!!

/**********************************************/
Option 2)
B. int x = t.findLarger(123, new Double(456));

Here
123 ---> Integer ---> Number
456 ---> Double ----> Number

But problem is that we can not call : Number.compareTo(Number)
type mismatch between Number and comparable<?> --> Comparable< ? extends Object>

/*******************************************************/
Option 3)
C. int x = t.findLarger(123, new Integer(456));

since both are Integer here..so T refers to Integer..so rest is Obvious..

/****************************************************/

Option 4)
D. int x = (int) t.findLarger(new Double(123), new Double(456));

T refers to Double..
so return type is also Double..!!
and we can not convert double to int..inconvertible type..!!!
/*************************************************/

this all explanation was as per my understanding on the issue..!!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Lata Bagga
Ranch Hand

Joined: Jun 13, 2007
Posts: 35
Thanks to all now I got the concept. Sunny you cleared all my doubts regarding this question. Thank you so much!!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Generics question
 
Similar Threads
doubt on wrappers
Mock Exam doubt Generics
Please help... Black magic java generics
Generics SCJP 1.5
probem with comparable interface