• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Narrowing Problem

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
http://radio.javaranch.com/corey/2004/06/01/1086120368000.html
link gives the best explanation. Please go through this article

Thanks,
Rancy
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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).

 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic