double field type declaration as zero

帅比萌擦擦* 提交于 2019-12-11 11:58:29

问题


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:

  1. Digits . DigitsoptExponentPartoptFloatTypeSuffixopt
  2. . Digits ExponentPartoptFloatTypeSuffixopt
  3. Digits ExponentPart FloatTypeSuffixopt
  4. Digits ExponentPartoptFloatTypeSuffix

...

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!