Thursday, 15 August 2013

Generics method Calling Behaviour

Generics method Calling Behaviour

public class Test {
public <T extends Comparable> T findLarger(T x, T y) {
if(x.compareTo(y) > 0) return x;
else 6. return y;
}
}
Test t = new Test();
// insert code here
Which two will compile without errors when inserted at line 23? (Choose two.)
A. Object x = t.findLarger(123, "456");
B. int x = t.findLarger(123, new Double(456));
C. int x = t.findLarger(123, new Integer(456));
D. int x = (int) t.findLarger(new Double(123), new Double(456));
My Doubt: I was taught that when a generic method is called the type is
replace by the called type.
for option A - when findLarger(int,String) is called what replaces t. if
Integer and how String works for the same methods and vice versa.

No comments:

Post a Comment