If I do the following
double d = 0;
since 0 is an integer literal, which uses 32 bits, and d is a double variable that uses 64 bits, will the remaining 32 bits be filled with random garbage, or does Java promote the literal correctly?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Java promotes it correctly, otherwise there’d be a rather large body of code that was problematic 🙂
Section 5.1.2 of the Java language spec details this:
Converting from a 32-bit Java
intto adouble(which, in Java, has 50+ bits of precision), will not lose the magnitude or any precision. If the constant you use is forced to alongdue to its value, you may lose precision, since alonghas 64 bits of precision.