What is the data type for the number 9.6352
in Java?
Actually if I postfix the number with 'd' or 'f'. then it will be quite easy to decide. But if I leave it as alone, then which will be the default type of the number in java?
Quote from Java Language Specification #3.10. 2: A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise, its type is double and it can optionally be suffixed with an ASCII letter D or d.
float has 7 decimal digits of precision. double is a 64-bit IEEE 754 double precision Floating Point Number – 1 bit for the sign, 11 bits for the exponent, and 52* bits for the value. double has 15 decimal digits of precision.
Size: Float is of size 32 bits while double is of size 64 bits. Hence, double can handle much bigger fractional numbers than float. They differ in the allocation of bits for the representation of the number.
Explanation: Given 3.14 is a double constant.
Go check out the JLS section on floating point literals (§3.10.2):
A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.
So it's a double if it's not suffixed.
By default, any floating point data type without 'f' or 'F' postfixed, is considered as of type double
.
Since the given number doesn't end with 'f'/'F', it is of type double
.
We can optionally add 'd' or 'D' at the end to mark it as a double
type, but again, it is optional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With