简体   繁体   English

浮点文字,浮字面,双字面

[英]Floating point literal, floating literal, double Literal

Is the 'floating point literal' and the 'floating literal' the same thing in Java? Java中的'浮点字面值'和'浮动字面值'是一样的吗?

Also, is there anything called 'double literal' in Java? 另外,Java中有什么叫做“双字面”吗?

"Floating point literal" refers to any literal of a floating-point type. “浮点文字”是指任何浮点类型的文字。 Both float and double are floating-point types. floatdouble都是浮点类型。

I have never come across the term "floating literal" before, but I would presume the intended meaning is the same as "floating point literal." 我之前从未遇到过“浮动文字”这个术语,但我认为其意图与“浮点文字”相同。 You should make sure to ask for clarification though, because if the author intended to say "float literal" then that would be a literal of type float in particular, and not just any floating-point type in general. 你应该确保要求澄清,因为如果作者打算说“float literal”,那么特别是float类型的文字,而不仅仅是任何浮点类型。

As for the specific type of floating-point literals in Java, all literals of the general form #.# are double , unless you append f or F (same difference) to the literal to force it to be float instead. 对于Java中特定类型的浮点文字,一般形式#.#所有文字都是double ,除非你将fF (相同的差异)附加到文字以强制它float

Check out the tutorial page on Primitive Data Types by Oracle. 查看Oracle的原始数据类型教程页面。 I've copied the sub-section titled "Floating-Point Literals" below. 我已经复制了下面标题为“浮点字面值”的子节。

Floating-Point Literals 浮点文字

A floating-point literal is of type float if it ends with the letter F or f; 如果浮点字面值以字母F或f结尾,则浮点字面值为float类型; otherwise its type is double and it can optionally end with the letter D or d. 否则它的类型是双倍的,它可以选择以字母D或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). 浮点类型(浮点数和双精度)也可以用E或e(科学计数法),F或f(32位浮点数字)和D或d(64位双字面值)表示;这是默认值和约定被省略)。

 double d1 = 123.4; // same value as d1, but in scientific notation double d2 = 1.234e2; float f1 = 123.4f; 

And as @Bubletan mentioned in comment to your question, if you're really curious you could check out the actual Java specification for float-point literals: §3.10.2. 正如@Bubletan在评论中提到的那样,如果你真的很好奇,你可以查看浮点文字的实际Java规范: §3.10.2。 Floating-Point Literals 浮点文字

Examples of float literals: float文字的示例:

 1e1f 2.f .3f 0f 3.14f 6.022137e+23f 

Examples of double literals: double文字的例子:

 1e1 2. .3 0.0 3.14 1e-9d 1e137 

Java has both float literals marked with a f suffix (for example, 2.5f) and double literals, which don't require a suffix (for example 2.5). Java有float文字标记有f后缀(例如,2.5f)和double文字,它们不需要后缀(例如2.5)。

The difference between the two can be demonstrated when you attempt to assign such literals to variables : 当您尝试将这些文字分配给变量时,可以证明两者之间的差异:

double dl = 2.5; // passes compilation
float fl1 = 2.5; // error - "Type mismatch: cannot convert from double to float"
float fl2 = 2.5f; // passes compilation

Both can be considered as floating point literals, since both double and float are floating point types. 两者都可以被认为是浮点文字,因为doublefloat都是浮点类型。

"floating-point literal" will be float type if it ends with F or f ; 如果以Ff结尾,“浮点字面量”将为浮点类型; otherwise it is a double 否则它是双倍的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM