java - 在 Java 中定义 double 文字时使用 'd' 是一个好习惯吗?

标签 java double literals

嗨,如果我想定义我写的双文字:

(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;

因此,添加 Dd 与省略是一样的,正如您已经知道的那样,但我想说,最好遵循约定并省略它。

关于java - 在 Java 中定义 double 文字时使用 'd' 是一个好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26066644/

相关文章:

java - 可以使#currentTimeMillis 成为常数吗?什么时候给它加值?

java - 如何关闭 HttpsURLConnection Java

java - 列表::包含比较器

swift - 在 swift 中比 double 更精确

java - 使用十六进制表示法的 byte[] 数组的文字语法..?

c# - 用 001 和 1 初始化 int 有区别吗?

c# - Java BouncyCaSTLe AES 解密的 C# 等价物是什么?

C#。 double 的奇怪行为

java - 如果用户输入了字符/字符串而不是数字,如何询问用户是否要重新启动程序?

c - C语言中如何确定无符号长整型的范围?