aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting 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 "casting" Watch "casting" New topic
Author

casting

pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
can anything be done to make this code compile?

[ April 18, 2004: Message edited by: Barry Gaunt ]

Thanks,<br />Pallavi
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Ask yourself: what type is numbers[0]?
Does it have a method intValue()?
Can you cast it to a type which has an intValue() method?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
ys i know numbers[0] is an object and Object class doenst have intValue() still was wondering if any thing could be done to fool the compiler
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What type is numbers[0] really? What type of object did you construct and stick in numbers[0]? It was the object referenced by the variable d.
[ April 18, 2004: Message edited by: Barry Gaunt ]
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
at compile time numbers[0] has a reference to d which is Double and at runtime it has Object am i ryt?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
At runtime numbers[0] is really referring to the variable d which is a Double. To tell the compiler it is really a Double you can cast numbers[0] to a Double before you call intValue() on it.
In fact, you can cast it to a Number which is the superclass of Double, Float, and, Integer. Now, because Number also has an intValue() method implemented by all of Double, Float, and Integer, you can replace numbers[0] by numbers[ii] for ii=0,1,2.

[ April 18, 2004: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting