java 为什么在浮点数末尾使用字母 f?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13704028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:42:32  来源:igfitidea点击:

Why is the letter f used at the end of a float no.?

java

提问by crvineeth99

I just wanted some information about this.

我只是想要一些关于这方面的信息。

float n = 2.99944323200023f

What does the fat the end of the literal do? What is it called? Why is it used?

什么是f在的结尾文字呢?这叫什么?为什么使用它?

回答by Michael Berry

The findicates it's a floating point literal, not a double literal (which it would implicitly be otherwise.) It hasn't got a particular technical name that I know of - I tend to call it the "letter suffix" if I need to refer to it specifically, though that's somewhat arbitrary!

f表明它是一个浮点文字,不是双字面它并没有一个特定的技术名称,我知道(这将暗示以其它方式) -我倾向于把它称为“字母后缀”如果我需要参考具体来说,虽然这有点武断!

For instance:

例如:

float f = 3.14f; //Compiles
float f = 3.14;   //Doesn't compile, because you're trying to put a double literal in a float without a cast.

You could of course do:

你当然可以这样做:

float f = (float)3.14;

...which accomplishes near enough the same thing, but the F is a neater, more concise way of showing it.

...几乎可以完成相同的事情,但 F 是一种更简洁、更简洁的显示方式。

Why was double chosen as the default rather than float? Well, these days the memory requirements of a double over a float aren't an issue in 99% of cases, and the extra accuracy they provide is beneficial in a lot of cases - so you could argue that's the sensible default.

为什么选择双重作为默认值而不是浮动值?嗯,如今,在 99% 的情况下,双精度浮点数的内存要求都不是问题,而且它们提供的额外准确性在很多情况下都是有益的 - 因此您可能会争辩说这是合理的默认值。

Note that you can explicitly show a decimal literal as a double by putting a dat the end also:

请注意,您还可以通过d在末尾放置 a 来显式地将十进制文字显示为双精度:

double d = 3.14d;

...but because it's a double value anyway, this has no effect. Some people might argue for it advocating it's clearer what literal type you mean, but personally I think it just clutters code (unless perhaps you have a lot of float literals hanging around and you want to emphasise that this literal is indeed meant to be a double, and the omission of the f isn't just a bug.)

...但因为无论如何它都是双值,所以这没有效果。有些人可能会争辩说它更清楚你的意思是什么文字类型,但我个人认为它只是混乱代码(除非你有很多浮点文字闲逛并且你想强调这个文字确实意味着是一个双重,并且省略 f 不仅仅是一个错误。)

回答by Sumit Desai

The default Java type which Java will be using for a float variable will be double. So, even if you declare any variable as float, what compiler has to actually do is to assign a double value to a float variable, which is not possible.So, to tell compiler to treat this value as a float, that 'f' is used.

Java 将用于浮点变量的默认 Java 类型将是 double。因此,即使您将任何变量声明为浮点数,编译器实际要做的就是将双精度值分配给浮点数变量,这是不可能的。因此,要告诉编译器将此值视为浮点数,即 'f'用来。

回答by Sunmit Girme

In Java, when you type a decimal number as 3.6, its interpreted as a double. doubleis a 64-bit precision IEEE 754 floating point, while floatis a 32-bit precision IEEE 754 floating point. As a floatis less precise than a double, the conversion cannot be performed implicitly.

在 Java 中,当您将十进制数输入为 时3.6,它会被解释为double. double是 64 位精度 IEEE 754 浮点数,而float是 32 位精度 IEEE 754 浮点数。由于 afloat不如 a 精确double,因此无法隐式执行转换。

If you want to create a float, you should end your number with f(i.e.: 3.6f).

如果你想创建一个浮点数,你应该以f(即:)结束你的数字3.6f

For more explanation, see the primitive data types definition of the Java tutorial.

有关更多解释,请参阅Java 教程原始数据类型定义

回答by Josh Greifer

It's to distinguish between floating point and double precision numbers. The latter has no suffix.

它是为了区分浮点数和双精度数。后者没有后缀。

回答by Marcin Orlowski

When you write 1.0, it's ambiguous as to whether you intend the literal to be a float or double. By writing 1.0f, you're telling Java that you intend the literal to be a float, while using 1.0d specifies that it should be a double (which is also default if you do not specify that explicitely).

当您编写 1.0 时,不清楚您希望文字是浮点数还是双精度数。通过编写 1.0f,您告诉 Java 您希望文字是浮点数,而使用 1.0d 指定它应该是双精度数(如果您没有明确指定,这也是默认值)。

回答by The Cat

You need to put the 'f' at the end, otherwise Java will assume its a double.

您需要将 'f' 放在最后,否则 Java 将假定它是双精度的。

回答by Esailija

It means that it's a single precision floating pointliteral rather than double precision. Otherwise, you'd have to write float n = (float)2.99944323200023;to cast the double to single.

这意味着它是一个单精度浮点文字而不是double precision。否则,您必须编写float n = (float)2.99944323200023;将双精度转换为单精度。

回答by jlordo

From the Oracle Java Tutorial, section Primitive Data Typesunder Floating-Point Literals

来自Oracle Java 教程的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.

如果浮点文字以字母 F 或 f 结尾,则它属于 float 类型;否则它的类型是 double 并且可以选择以字母 D 或 d 结尾。

回答by Mik378

If fis not precised at the end, value is considered to be a double. And a doubleleads to more bytes in memory than float.

如果f最后没有精确,则 value 被认为是 a double。并且 adouble导致内存中的字节数比float.