Java 为什么 f 放在浮点值之后?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9748160/
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
why f is placed after float values?
提问by ipkiss
I don't know why f or F is placed after float values in Java or other languages? for instance,
我不知道为什么在 Java 或其他语言中将 f 或 F 放在浮点值之后?例如,
float fVariable = 12.3f;
any features other than indicating that this is a float value?
除了表明这是一个浮点值之外还有什么特征?
采纳答案by Jigar Joshi
by default 12.3
is double
literal, So to tell compiler to treat it as float
explicitly -> it uses f
or F
默认情况下12.3
是double
文字,所以告诉编译器float
显式地对待它- >它使用f
或F
回答by Peter Lawrey
float
and double
can only provide approximate representation values for some values. e.g. 12.3 or 0.1
float
并且double
只能为某些值提供近似的表示值。例如 12.3 或 0.1
The difference is that float is not as accurate (as it has less precision, because its smaller)
不同之处在于 float 不那么准确(因为它的精度较低,因为它较小)
e.g.
例如
System.out.println("0.1f == 0.1 is " + (0.1f == 0.1));
System.out.println("0.1f is actually " + new BigDecimal(0.1f));
System.out.println("0.1 is actually " + new BigDecimal(0.1));
prints
印刷
0.1f == 0.1 is false
0.1f is actually 0.100000001490116119384765625
0.1 is actually 0.1000000000000000055511151231257827021181583404541015625
So 0.1
is the closest representation in double
and 0.1f
is the closest representation in float
所以0.1
是最接近的表示double
,0.1f
是最接近的表示float
回答by kundan bora
float fVariable = 12.3; is fine. but when you use only float value(without any identifier) in any expression that time you need to tell compiler that value is float hence we use suffix "f" after value. example
浮动 fVariable = 12.3; 很好。但是当您在任何表达式中仅使用浮点值(没有任何标识符)时,您需要告诉编译器该值是浮点数,因此我们在值后使用后缀“f”。例子
float fl =13f/5f;
浮动 fl = 13f/5f;
here 13 and 5 are float values.
这里 13 和 5 是浮点值。
回答by Perception
Seeing as there are only so many ways to represent a number in your program, the designers of Java had to cherry pick and assign each form to the most common use case. For those forms selected as default, the suffixthat denotes the exact type is optional.
鉴于在您的程序中表示数字的方式只有这么多,Java 的设计者不得不仔细挑选每种形式并将其分配给最常见的用例。对于默认选择的那些表单,表示确切类型的后缀是可选的。
- For Integer literals (int, long), the default is int. For obvious reasons.
- For Floating point literals (float, double) the default is double. Because using double potentially allows saferarithmetic on the stored values.
- 对于整数文字 (int, long),默认值为int。出于显而易见的原因。
- 对于浮点文字 (float, double),默认值为double。因为使用 double 可能允许对存储的值进行更安全的算术。
So, when you type 12
in your program, thats an intliteral, as opposed to 12L
, which is a long.
And when you type 12.3
, thats a doubleliteral, as opposed to 12.3F
, which is a float.
所以,当你输入12
你的程序时,它是一个int字面量,而不是12L
一个long。当你输入时12.3
,这是一个双精度文字,而不是12.3F
,它是一个浮点数。
So where is this relevant? Primarily in handling downcasts, or narrowing conversions. Whenever you downcast a long to an int, or a double to a float, the possibility for data loss exists. So, the compiler will force you to indicate that you reallywant to perform the narrowing conversion, by signaling a compile error for something like this:
那么这有什么关系呢?主要用于处理向下转换或缩小转换。每当您将 long 转换为 int 或将 double 转换为浮点数时,都存在数据丢失的可能性。因此,编译器将通过发出编译错误信号来强制您表明您确实要执行收缩转换,如下所示:
float f = 12.3;
Because 12.3 represents a double, you have to explicitly cast it to a float (basically signing off on the narrowing conversion). Otherwise, you could indicate that the number is really a float, by using the correct suffix;
因为 12.3 代表一个双精度值,所以您必须将其显式转换为浮点数(基本上是在缩小转换上签字)。否则,您可以通过使用正确的后缀来表明该数字确实是一个浮点数;
float f = 12.3f;
So too summarize, having to specify a suffix for longsand floatsis a compromise the language designers chose, in order to balance the need to specify what exactly a number is, with the flexibility of converting numbers from one storage type to another.
总结一下,必须为longs和floats指定后缀是语言设计者选择的一种折衷方案,以平衡指定数字到底是什么的需要,以及将数字从一种存储类型转换为另一种存储类型的灵活性。
回答by User123456
Float is single-precision 32-bit IEEE 754 floating point and Double is double-precision 64-bit IEEE 754 floating point. When you use a value with decimal points and if you don`t specify is as 0.23f (specifically float) java identifies it as a double.
Float 是单精度 32 位 IEEE 754 浮点数,Double 是双精度 64 位 IEEE 754 浮点数。当您使用带小数点的值并且如果您没有指定为 0.23f(特别是浮点数)时,java 会将其识别为双精度值。
For decimal values, double data type is generally the default choice taken by java. Check This
对于十进制值,double 数据类型通常是 java 所采用的默认选择。 检查这个
回答by Confuse
During compilation, all floating point numbers (numbers with decimal point) default to double.
在编译过程中,所有浮点数(带小数点的数字)默认为double。
Therefore, if you don't want your number to double and just want it as float, you have to explicitly tell the compiler by adding a for Fat end of the literal constant.
因此,如果您不希望您的数字加倍而只想将其作为浮点数,您必须通过在文字常量的末尾添加f或F来明确告诉编译器。
回答by Geetika Agarwal
In java we have many different basic variable types so in order to make it general , it has some default features. Like if we give an input 16.02 it automatically takes it as a double input. So if you want to specify it as float we mention that 'f'after the number or we can simply use:
在 Java 中,我们有许多不同的基本变量类型,因此为了使其通用,它具有一些默认功能。就像我们给输入 16.02 一样,它会自动将其作为双重输入。因此,如果您想将其指定为浮点数,我们会在数字后提及“f”,或者我们可以简单地使用:
float f = (float) 16.02;
float f = (float) 16.02;
or
或者
float f = 16.02f;
float f = 16.02f;
In the same way we have to mention 16lif we want the number to be saved as a long type else it will automatically select the default type ie int type.
同样,如果我们希望将数字保存为 long 类型,则必须提及 16 l,否则它将自动选择默认类型,即 int 类型。
回答by Chetan Raj
If you do not use f it will be interpreted as double, which is the default in Java. You can also write it like this##
如果不使用 f ,它将被解释为双精度值,这是 Java 中的默认值。也可以这样写##
float f=(float) 32.5956;
float f=32.5956f;
float f=(float) 32.5956;
float f=32.5956f;