• 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

conversion of primitives scjp

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai
the conversion between long to float is allowed in java, the size of long is 64 were as the size of the float is 32 how is it possible, would'nt there be loss in the data, plz give me adetailed explanation.
if possible with as example.
thanking you
shyam
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kumar shyam:
the conversion between long to float is allowed in java, the size of long is 64 were as the size of the float is 32 how is it possible, would'nt there be loss in the data, plz give me adetailed explanation.
if possible with as example.


If you convert one type of data into another the rules of casting must be followed. If with conversion you lose precision, as when converting an int to a byte, then you will get a compiler error unless you explicitly cast.
A data type with fewer bits can be converted to a type with more bits without explicit casting. When you downcast a value of a wider type the upper bytes will be truncated.
So for example, the lowest order byte in the int value will be copied to the byte value.
In the case of long to float conversion where the target allows for bigger numbers a large value in the long type value that uses all 64 bits will lose some of the lower bits when converted to float since the exponent uses 8 bits of the 32 provided for float values.
I hope this helps
Greetings,
Gian Franco
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gian Franco Casula:

In the case of long to float conversion where the target allows for bigger numbers a large value in the long type value that uses all 64 bits will lose some of the lower bits when converted to float since the exponent uses 8 bits of the 32 provided for float values.


But why if I run this code:

the result is true! Did I miss something here?
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Yes you got a tricky example , because the
number can be represented in a float.
Try the following instead (look at line #1):

The result will be false
Greetings,
Gian Franco
 
Yosi Hendarsjah
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right. I didn't try the code with other values.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic