programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Devaka Cooray
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• paul wheaton
• Henry Wong
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Carey Brown
• Mikalai Zaikin
Bartenders:
• Lou Hamers
• Piet Souris
• Frits Walraven

# Narrowing Problem

Ranch Hand
Posts: 344
• Number of slices to send:
Optional 'thank-you' note:

In the above coding, m(a1) a1 is int which is 32 bit and passing to float which is also 32 bit.so no problem.
But in m(a2), a2 is long (64 bit) and passing to a float (32 bit), which is narrowing. How it's returing the float value without explicit cast?
Why can't for m(a2), the compiler should choose the double, since both long and double are 64 bit?

Ranch Hand
Posts: 135
• Number of slices to send:
Optional 'thank-you' note:
Hi Michael,

The output you are getting is correct. The reason is that long does get widened to float implicitly. The widening functionality works like this:

byte-->short------>
int ---> long---> float----> double
char------->

Now you would be wondering how long(64 bits) gets converted to float (32 bits) implicitely. For this,

Thanks,
Rancy

Ranch Hand
Posts: 7729
• Number of slices to send:
Optional 'thank-you' note:
"a2 is long (64 bit) and passing to a float (32 bit), which is narrowing"

This is plain wrong. long is convertable to float because the range of a long is accomodated by a float.

Take a good look at what the JLS says:

5.1.2 Widening Primitive Conversion
The following 19 specific conversions on primitive types are called the widening primitive conversions:

* 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

Widening primitive conversions do not lose information about the overall magnitude of a numeric value.

<SNIP>

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (�4.2.4).

Ranch Hand
Posts: 135
• Number of slices to send:
Optional 'thank-you' note:
Sorry about that widening diagram in prior post, what I meant is

byte
|
short char
| |
--------
|
int
|
long
|
float
|
double

Micheal John
Ranch Hand
Posts: 344
• Number of slices to send:
Optional 'thank-you' note:
Thanks for your reply.. Now I understood and I think I have to go thru the entire JLS one time..but it is diffcult to go thru' it as the JLS seems to be very hard to understand, since it's not written in the plain english.

arch rival
Posts: 2813
• Number of slices to send:
Optional 'thank-you' note:
Whilst the JLS is good for "argument settling", you generally don't need to go through it for SCJP purposes. Just read one of the standard texts aimed at that exam (e.g. Sierra and Bates) very carefully and you should be OK.

 We're all out of roofs. But we still have tiny ads: We need your help - Coderanch server fundraiser https://coderanch.com/wiki/782867/Coderanch-server-fundraiser