aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting int to long 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 int to long " Watch "Casting int to long " New topic
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
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3218
    
  49

The ordering is, byte->short->int->long->float->double.

That is the basic reason of this output.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- 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
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

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
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Casting int to long