• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please help... Black magic java generics

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're misinterpreting the error that's reported. The compiler is complaining about trying to assign the output of the method to an int, not about the parameters you send to it.

In option A, both parameters are acceptable and the return type can be assigned to an Object.

In option B, both parameters are acceptable, but since one is autoboxed to an Integer and the other is a Double, it appears that both are being promoted to their superclass Number, and the output is not an Integer, so you can't autounbox it to an int.

In option C, both parameters are Integer, so the output of the method is Integer, which can be autounboxed to an int.

In option D, both parameters are Double, so the output of the method is a Double, which can't be cast to an int.

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));


[ April 17, 2006: Message edited by: Keith Lynn ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And about the compareTo() thing, if you run the code you will find that a runtime ClassCastException is thrown. Compiles though and thats what the question asks you
 
Marx Villegas
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!
I understand now. I guess I had the vision that under the hood the compiler replaced T with int, or whatever the first T is.
Thanks again.
Marx
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice you get an unchecked warning for this code, which is telling you that something is wrong.
Try changing the line:
public <T extends Comparable> T findLarger(T x, T y) {

to
public <T extends Comparable<T>> T findLarger(T x, T y) {
[ April 17, 2006: Message edited by: Barry Gaunt ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic