Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting int to long

 
Jeena Jeen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Marshal
Pie
Posts: 4502
268
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ordering is, byte->short->int->long->float->double.

That is the basic reason of this output.
 
Jeena Jeen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ouch... it was so simple.
Thanks a lot
 
armando fonseca
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, char -> int -> foat -> double
 
Shin Kudo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeena Jeen wrote:



How come does this code produce output? It doesn't even compile!
 
Albert Kam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeena Jeen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Shin I just copied the relevant part of the code. The complete code is:
 
Jeena Jeen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the correct way to depict implicit type cast is this




You don't need to have separate chains for numeric types and char
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's exactly how I think about it. The picture basically condenses all possible transitions allowed for primitive widening conversion.
 
armando fonseca
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic