Java 中的“浮点字面量”和“浮点字面量”是一回事吗?
此外,Java 中是否有所谓的“双重文字”?
最佳答案
“浮点字面量”是指任何浮点类型的字面量。 float
和double
都是浮点类型。
我以前从未遇到过“浮点文字”一词,但我认为其预期含义与“浮点文字”相同。 你应该确保要求澄清, 因为如果作者打算说“float literal”那么那将是 float
类型的文字,而不仅仅是一般的任何浮点类型。
对于Java中浮点字面量的具体类型,一般形式#.#
的字面量都是double
,除非你追加f
或 F
(相同的区别)到文字以强制它改为 float
。
查看 Primitive Data Types 上的教程页面甲骨文。我复制了下面标题为“浮点文字”的小节。
Floating-Point Literals
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;
正如@Bubletan 在对你的问题的评论中提到的,如果你真的很好奇,你可以查看浮点文字的实际 Java 规范:§3.10.2. Floating-Point Literals
Examples of
float
literals:1e1f 2.f .3f 0f 3.14f 6.022137e+23f
Examples of
double
literals:1e1 2. .3 0.0 3.14 1e-9d 1e137
关于java - 浮点字面量、浮点字面量、 double 字面量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41289696/