• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implicit downcast possible?

 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the following code, the question is asked, what will be the output? The answer is a compile time error. But the thing is, i thought it was possible for some implicit downcasting to occur - in what situations?
public class Rand{
public static void main(String argv[]){
int iRand;
iRand = Math.random();
System.out.println(iRand);
}
}
 
El Roy Cabildo
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of course it would compile error because Math.random() returns double not int! Loss of precission and java wont allow you that
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yo, very true elroy ... i guess i was just wondering whether there where any similar-yet-different situations ... where implicit downcasting might occur>?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final int i = 5;
byte b = i;
In this case the "final" tells the compiler that i can't change and therefore it will always fit into a byte.
[ February 19, 2003: Message edited by: Thomas Paul ]
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, that's right, thankyou!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic