Search code examples
javavariablesdoubledeclarationzero

double field type declaration as zero


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.


Solution

  • There is no difference. Have a look at the Java Language Specification, section 3.10.2

    DecimalFloatingPointLiteral:

    1. Digits . Digitsopt ExponentPartopt FloatTypeSuffixopt
    2. . Digits ExponentPartopt FloatTypeSuffixopt
    3. Digits ExponentPart FloatTypeSuffixopt
    4. Digits ExponentPartopt FloatTypeSuffix

    ...

    FloatTypeSuffix: one of

    f F d D
    

    Both are a DecimalFloatingPointLiteral, the first one type 1, the second one type 4