• 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

Convertions

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...
I was doing a mock test and I didn't understood how it works:
short i = (short)Float.NEGATIVE_INFINITY;
short j = (short)Float.POSITIVE_INFINITY;
short k = (short)Float.NaN;
when run this the values are: i=0, j=-1 and k=0.
Can anyone explain it to me ? Thanks!
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Isabel Wanderley:
Hi...
I was doing a mock test and I didn't understood how it works:
short i = (short)Float.NEGATIVE_INFINITY;
short j = (short)Float.POSITIVE_INFINITY;
short k = (short)Float.NaN;
when run this the values are: i=0, j=-1 and k=0.
Can anyone explain it to me ? Thanks!


When a floating point number is converted to a type T, it is first converted to an int if T is a byte, short, char, or int. If the float value is more negative than Integer.MIN_VALUE, then the float value is converted to Integer.MIN_VALUE. When Integer.MIN_VALUE is subsequently converted to type short it becomes zero.
The hexadecimal representation of Integer.MIN_VALUE is 0x80000000. When 0x80000000 is converted to a short it becomes zero becuase only the low order 16 bits are used.
A similar process is used to convert Float.POSITIVE_INFINITY to a short. First Float.POSITIVE_INFINITY is converted to Integer.MAX_VALUE which is 0x7FFFFFFF. When 0x7FFFFFFF is converted to a short it becomes 0xFFFF which is the two's compliment representation of -1.
When Float.NaN is converted to an integral value it always becomes zero.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see The Java Language Specification, Section 5.1.3, Narrowing Primitive Conversions.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one of the most surprising (mis)features of Java that I encountered when studying for the exam. I don't know why they designed the language this way, narrowing first to int, rather than directly narrowing from float to the desired type.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic