I don't know you guys, but I do not like Generics. I have been trying to figure this question from a mock exam:
1. public class
Test {
2. public <T extends Comparable> T findLarger(T x, T y) {
3. if(x.compareTo(y) > 0) {
4. return x;
5. } else {
6. return y;
7. }
8. }
9. }
and:
22. Test t = new Test();
23. // 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));
the answers are A & C. Let's review option A:
A. Ok, Object is not comparable, but a comparable is an object, that I can understand, so it fits the reference variable Object. But how can a
String be entered and casted to be compared with an integer? I did some further testing and it results that you can stuff in almost any class that implements Comparable and compare it with the 123 int. My question is, why can you enter a String, and any other comparable class, but not a Double?
Thanks in advance
Marx