• 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

Implicit cast to double from long

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading Sun Certified Programmer & Developer for Java2, and in it I found the following:


double d = 100L; // Implicit cast
In the preceding statement, a double is initialized with a long value (as denoted by the L after the numeric value). No cast is needed in this case because a double can hold every piece of information that a long can store


But if a double is 64 bits in size and has to look after a potential exponent, how could it maintain the same precision as a long? I haven't seen anything in the errata for this, so I assume it is right and that there is something going on here I don't understand.
Could someone clarify this for?
Thanks
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is from jls 2.0 chap 5.1.2., hope it clarifies...
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).
Despite the fact that loss of precision may occur, widening conversions among primitive types never result in a run-time exception (�11).
Here is an example of a widening conversion that loses precision:
class Test {
public static void main(String[] args) {
int big = 1234567890;
float approx = big;
System.out.println(big - (int)approx);
}
}
which prints: -46
 
Sean Walker
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
Thank you, that was quite helpful. But it also sort of suggests to me what we used to say to each other back in University - RTFM! I should have a look at the spec more often. I just find that specs are usually incomprehensible...
Anyway, from what you have quoted here it seems that the author was wrong. Specifically, he was incorrect in his explanation that 'a double can hold every piece of information that a long can store'. I don't think I'm being anal in thinking that he's excluding the possibility that precision would be dropped when casting from a double in this assertion.
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that by "can hold a long" the author meant that is not needed an explicit cast.
However he should've pointed out the loss of precission problem that can occur in some cases.
I agree with you that the JLS is at times incomprehensible but I use it a lot in preparing for the SJCP, because I noticed over the years that many books(not just for Java) do not have an all around very good explanation of the language missing especially on the little details (your example is a good one).
 
Everybody's invited. Except this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic