问题
What is significant difference between following zero double variable declaration:
0.0
VS 0d
double d1 = 0.0;
double d2 = 0d;
I understand that both these are better than just 0
,because are more obvious for code reader.
However 0.0
VS 0d
difference is not clear for me.
回答1:
There is no difference. Have a look at the Java Language Specification, section 3.10.2
DecimalFloatingPointLiteral:
Digits . Digits
optExponentPart
optFloatTypeSuffix
opt. Digits ExponentPart
optFloatTypeSuffix
optDigits ExponentPart FloatTypeSuffix
optDigits ExponentPart
optFloatTypeSuffix
...
FloatTypeSuffix: one of
f F d D
Both are a DecimalFloatingPointLiteral
, the first one type 1, the second one type 4
回答2:
Go check out the JLS section on floating point literals:
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
来源:https://stackoverflow.com/questions/16543025/double-field-type-declaration-as-zero