Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes implicit downcast possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "implicit downcast possible?" Watch "implicit downcast possible?" New topic

implicit downcast possible?

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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();

giddee up
El Roy Cabildo

Joined: Oct 19, 2002
Posts: 23
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

Joined: Jan 10, 2003
Posts: 284
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

Joined: May 05, 2000
Posts: 13974
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 ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
ah, that's right, thankyou!
I agree. Here's the link:
subject: implicit downcast possible?
It's not a secret anymore!