Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparable 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 "Comparable" Watch "Comparable" New topic
Author

Comparable

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178


Dear friends,
Why does the variable x2 (Line 5) produce compiler error?
What exactly T is? Is it equivalent to E?

Kindly provide your help.
Regards,
Vijay
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Hi !!!


is not compiling because the method is waiting for two arguments of the same type. You give him an int and a Double.
Note that integer litteral are considered like int. Thus 123 will be boxed as an Integer.

You should write this:


SCJP 5.0 | SCWCD 1.4
Arek Orzechowski
Greenhorn

Joined: Nov 03, 2007
Posts: 1
Hi,

I agree it should be


but I think the reason the original line is not compiling are not the parameters (123 is boxed as Integer) but the result, which is of class Number (common superclass of Integer and Double) and cannot be converted to double

this compiles ok:
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Originally posted by Arek Orzechowski:
Hi,
....
this compiles ok:


I agree with you that the code will compile, because Number is superclass of Integer and Double. Since we are talking of compilation it's ok.

I will just add that the code will throw a ClassCastException if you don't turned 123 into a double even if it compiles.
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Thanks for the reply!!!




Regards,
Vijay
 
 
subject: Comparable
 
Similar Threads
Generics Error
probem with comparable interface
Generics SCJP 1.5
How to read this Generics method
Please help... Black magic java generics