java - 浮点字面量、浮点字面量、 double 字面量

标签 java

Java 中的“浮点字面量”和“浮点字面量”是一回事吗?

此外,Java 中是否有所谓的“双重文字”?

最佳答案

“浮点字面量”是指任何浮点类型的字面量。 floatdouble 都是浮点类型。

我以前从未遇到过“浮点文字”一词​​,但我认为其预期含义与“浮点文字”相同。 你应该确保要求澄清, 因为如果作者打算说“float literal”那么那将是 float 类型的文字,而不仅仅是一般的任何浮点类型。

对于Java中浮点字面量的具体类型,一般形式#.#的字面量都是double,除非你追加fF(相同的区别)到文字以强制它改为 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/

相关文章:

java - 是否可以使用Java检查hadoop集群是否启用了yarn?

java - 如何使用装饰器 DP 将 addDough 方法写入我的披萨

java - MVC REST API 中使用 Jackson 的动态 JSON 类型

java - 无法解析符号日历(Java 中的 Google 日历 api)

java - 交换方法中的对象引用

java - 如何定义一个自动获取重写方法的java注释?

java - 有人能解释一下为什么这个 While 语句必须是 AND

java - 为什么 Java 在数字提升期间将字节和短操作数转换为整数

java - 使用具有相对路径的 JasperReports

java - 如何删除Android状态栏自动颜色变化