java Java中浮点“1”和浮点“1f”之间的区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11323915/
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
What is the Difference between float "1" and float "1f" in Java?
提问by m4tx
Is there any difference - in Java - between defining float variables like, e.g., this: 1
and this: 1f
? Is the JVM doing any casts at run-time when I write 1
, or something that could slow down my app?
在 Java 中,定义浮点变量(例如 this:1
和 this: )之间有什么区别1f
吗?JVM 在运行时是否在执行任何强制转换1
,或者可能会减慢我的应用程序的速度?
Regards
问候
回答by Jigar Joshi
1 is implicitly considered as int
literal where 1f
is considered as float
literal
1 被隐式视为int
文字,其中1f
被视为float
文字
See
看
回答by vbezhenar
Java code
Java代码
float f1 = 1;
float f2 = 1f;
compiles to following bytecode:
编译为以下字节码:
0: fconst_1
1: fstore_1
2: fconst_1
3: fstore_2
As you can see, there's no difference at run-time.
如您所见,运行时没有区别。
回答by GuilhE
float a = 1;
float b = 1f;
It's the same, but if you do something like:
这是一样的,但如果你做这样的事情:
int a = 1f
Will throw "Type mismatch: cannot convert from float to int"
会抛出“类型不匹配:无法从浮点数转换为整数”
int b = 1d or 1.0
Will throw "Type mismatch: cannot convert from double to int"
会抛出“类型不匹配:无法从双精度转换为整数”
float c = 1d or 1.0
Will throw "Type mismatch: cannot convert from double to float"
Note that:
会抛出“Type mismatch: cannot convert from double to float”
注意:
double a = 2.0;
double b = 2d;
double c = 2.0f;
if (a == b) {
if (c == a) {
if (c == b) {
return 1;
}
}
}
Will return 1;
Regards.
将返回 1;
问候。
回答by Andreas Johansson
Basically 1
will default to an int
. If you write 1f
it will be considered a float
. If you write 1.0
(no f) it will default to a double
.
基本上1
会默认为int
. 如果你写1f
它会被认为是一个float
. 如果你写1.0
(no f) 它将默认为double
.
A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.
如果浮点字面量以 ASCII 字母 F 或 f 为后缀,则它属于 float 类型;否则它的类型是 double 并且可以选择以 ASCII 字母 D 或 d 为后缀。
回答by Crazenezz
If the case like this the JVM will pass it.
如果是这种情况,JVM 将通过它。
public class Test {
public static void main(String[] args) {
float f = 1;
System.out.println(f);
}
}
But it will occur exception if you do something like this:
但是如果你做这样的事情会发生异常:
public class Test {
public static void main(String[] args) {
float f = 1.5;
}
}
Exception:
例外:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - possible loss of precision
required: float
found: double
at package.Test.main(Test.java:17)
What we can analyze for this two example is, for the first example it automatically converted to float
, but for the second example if you add some decimal point without the f
or F
suffix than it will automatically converted to double
.
对于这两个例子,我们可以分析的是,第一个例子它自动转换为float
,但是对于第二个例子,如果你添加一些没有f
或F
后缀的小数点,它会自动转换为double
。
回答by manurajhada
Yes, there is a big difference between 1
and 1f
. As you can't declare a variable without giving its type in java, Its clear 1
is an int
and 1f
denotes float
at compile time itself. This is nothing related to run time or slow down your app.
是的,1
和之间有很大的区别1f
。正如你不能没有在Java中给予其类型声明一个变量,它明确1
是int
和1f
表示float
在编译时间本身。这与运行时间或减慢应用程序无关。