wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics - compiler failure that I do not understand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics - compiler failure that I do not understand" Watch "generics - compiler failure that I do not understand" New topic
Author

generics - compiler failure that I do not understand

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Given this code segment:



WHY does the "return x.doubleValue();" statement fail compilation?
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

All you're telling the compiler is that X extends Object, which is nothing because all classes extend Object. You need to say it extends a class that has a doubleValue() method, like Number.
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Greg Charles wrote:All you're telling the compiler is that X extends Object, which is nothing because all classes extend Object. You need to say it extends a class that has a doubleValue() method, like Number.


ok. But when I replace:



with this:



That will compile because I am telling the compiler to pretend that x is a Double, which DOES support the doubleValue() function
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Yes, that's true. In that case, you are telling the compiler that you promise you will only be using a Double value there. If you lie, you'll get a nice ClassCastException at run time. The compiler is trying to save you from problems like that before they happen, but if you're determined to shoot yourself in the foot, then you can!
Gajendra Kangokar
Ranch Hand

Joined: Dec 25, 2012
Posts: 65

but then when we replace "return x.doubleValue();" by "return ((Double)x).doubleValue();" it gives run time exception!!
how to solve this .

thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Gajendra Kangokar wrote: but then when we replace "return x.doubleValue();" by "return ((Double)x).doubleValue();" it gives run time exception!!
how to solve this .

thanks

Gajendra,
Please go through Greg's latest post. I guess you are facing the same ClassCastException.

Since X is Object, compiler will give error when you invoke doubleValue method on X (since doubleValue is not part of Object class).
When you cast X to Double, compiler will pass it, since doubleValue is method of Double class.
However, at runtime, if X cannot be cast to Double, then you will get a ClassCastException.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Gajendra Kangokar
Ranch Hand

Joined: Dec 25, 2012
Posts: 65

thank you Anayonkar
i understand now

1.at first "x extends Object" cannot recognize "return x.doubleValue()" so compiler gives error.

2.here "x extends Object" it tells compiler that at this line "return ((Double)x).doubleValue()"

you will receive Double datatype which compiler accepts.

But the user is passing Integer type so there is 'ClassCastException' at run time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics - compiler failure that I do not understand
 
Similar Threads
Simple generics - error in code
Generics Question: Why Won't This Code Compile?
Explain this generics code?
Generic class question
Problem in Generic Method Declaration