Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting

 
pallavi utukuri
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anything be done to make this code compile?

[ April 18, 2004: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
pallavi utukuri
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic