• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not able to understand the output  RSS feed

 
Aakash Chandel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output of the above program is: int float Object

Not sure how the output came. Can anybody please guide?
 
Shaikh Ali
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hope the comments help a little bit.
 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to understand the different primitive widening conversions. They are as follows
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double

If the argument in the calling function does not find a match then it would use a combination of widening, boxing and / or varargs to find the closest available match. Based on the above ....
1. The short s gets widened to an int and uses the method public void print(int x)
2. 12345L is a long that gets widened to a float and uses the method public void print(float x)
3. 6.789 is a double and uses the BoxAndWiden theory where it gets auto-boxed to a Double and then widened to an Object. So it uses public void print(Object x)
 
Maitri Solanki
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not understand why Long widens to Float. Long can store 8 bytes and float can store 4 bytes. So technically long is bigger than float. Am I right? Then why long widens to float?
 
Asad Zubair
Ranch Hand
Posts: 44
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The discussion below should hopefully clarify some doubts.

http://stackoverflow.com/questions/1293819/why-does-java-implicitly-without-cast-convert-a-long-to-a-float
 
Emmanuel Aron
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is "long 'widens' to float" and not "Long 'widens' to Float"....
 
Aakash Chandel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for the reply. This was very helpful.
 
Maitri Solanki
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Asad! The link explains everything!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!