嗨,如果我想定义我写的双文字:
(1) double a = 12.0;
但是,例如,当我定义小数点后没有任何内容的 double 时
(2) double b=12;
使用起来是否更容易被接受
(3) double c=12d;
相反?
据我所知,(1) 是最好的方法,如果我可以问你声明双文字的下一个更好的方法是什么,你会选择哪种方法,为什么?
最佳答案
摘自Java语言规范:
A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.
The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).
double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1 = 123.4f;
因此,添加 D
或 d
与省略是一样的,正如您已经知道的那样,但我想说,最好遵循约定并省略它。
关于java - 在 Java 中定义 double 文字时使用 'd' 是一个好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26066644/