Author
Casting int to long
Jeena Jeen
Ranch Hand
Joined: Feb 11, 2009
Posts: 47
The output of this code is "long". I am not sure why this output. I was expecting in to produce output "float". If I remove method 3 output becomes "float". Please can anyone explain what is the logic behind this?
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
The ordering is, byte->short->int->long->float->double.
That is the basic reason of this output.
Author of ExamLab (Download ) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Jeena Jeen
Ranch Hand
Joined: Feb 11, 2009
Posts: 47
ouch... it was so simple.
Thanks a lot
armando fonseca
Ranch Hand
Joined: Apr 03, 2009
Posts: 49
Also, char -> int -> foat -> double
scjp6-90%
Shin Kudo
Greenhorn
Joined: Apr 17, 2009
Posts: 25
Jeena Jeen wrote:
How come does this code produce output? It doesn't even compile!
SCJP 6 - SCWCD in progress...
Albert Kam
Ranch Hand
Joined: Oct 18, 2007
Posts: 58
Hello all,
I modified the source to make sure that char fits into an int
(both char n int use 32 bits, although char only makes use of 16 bits for it's value)
Regards,
Albert Kam
Be nice !
Jeena Jeen
Ranch Hand
Joined: Feb 11, 2009
Posts: 47
Sorry Shin I just copied the relevant part of the code. The complete code is:
Jeena Jeen
Ranch Hand
Joined: Feb 11, 2009
Posts: 47
armando fonseca wrote: Also, char -> int -> foat -> double
Hi armando !
I think You forgot long in the list. So for char it should be:
char ->int ->long -> float -> double
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
Actually the correct way to depict implicit type cast is this
You don't need to have separate chains for numeric types and char
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted Apr 21, 2009 07:35:21
0
That's exactly how I think about it. The picture basically condenses all possible transitions allowed for primitive widening conversion.
All code in my posts, unless a source is explicitly mentioned, is my own.
armando fonseca
Ranch Hand
Joined: Apr 03, 2009
Posts: 49
Jeena Jeen wrote:
armando fonseca wrote: Also, char -> int -> foat -> double
Hi armando !
I think You forgot long in the list. So for char it should be:
char ->int ->long -> float -> double
You are right!
and true , is only one chaining!
subject: Casting int to long