浮动变量初始化java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18425488/
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
float variables initialization java
提问by UnderDog
The following code snippet gave me compiler error at Line 1.
以下代码片段在第 1 行给了我编译器错误。
public abstract class xyz
{
float Gamma = 20.0; //Line 1
public class Alpha
{
void Beta()
{
System.out.println("this is atest");
}
}
}
abc0.java:5: error: possible loss of precision
float density = 20.0;
^
required: float
found: double
2 errors
As per my understanding, float is used for decimal variables.
根据我的理解, float 用于十进制变量。
Am I missing something obvious or should I have to always use casting for decimal variables initialization ?
我是否遗漏了一些明显的东西,还是我必须始终使用强制转换来进行十进制变量初始化?
EDIT : I know 'f' can be used at the end for float variables but is it MANDATORY ?.
编辑:我知道 'f' 可以在最后用于浮点变量,但它是强制性的吗?。
采纳答案by chrylis -cautiouslyoptimistic-
Floating-point literals are considered double
s unless you specify that they're just float
s. (Similarly, integer literals are int
s unless specified otherwise.) Append the letter f
to the number to make it a float
:
浮点文字被视为double
s,除非您指定它们只是float
s。(类似地,int
除非另有说明,否则整数文字是s。)将字母附加f
到数字后使其成为float
:
float density = 20.0f;
The JLS has comprehensive typing rulesfor literal values. No, you don't have to make the literal a float
with f
, but then you have to cast it with (float)
if you want to fit it in a float
variable, since Java won't automatically try to shove a number of one type into a variable with a smaller range.
JLS 具有全面的文字值类型规则。不,您不必将文字 a float
with f
,但是(float)
如果要将其放入float
变量中,则必须将其强制转换为较小的范围。
回答by Emanuel Saringan
should be
应该
float density = 20.0f;
回答by Mohammod Hossain
float density = 20.0f;
浮动密度 = 20.0f;
If you try to assign a decimal number you must place an "f" at the end, otherwise Java will assume you are trying to assign a double .A double would more precisely cover more numbers that you could type in.
如果您尝试分配一个十进制数,您必须在末尾放置一个“f”,否则 Java 会假设您正在尝试分配一个 double 。一个 double 会更准确地涵盖您可以输入的更多数字。
回答by Makoto
Per the JLS, §3.10.2, all floating point literals are interepreted as double
unless specified as a float
.
根据JLS, §3.10.2,double
除非指定为 a ,否则所有浮点文字都被解释为float
。
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 (§4.2.3).
如果浮点字面量以 ASCII 字母 F 或 f 为后缀,则它属于 float 类型;否则它的类型是 double 并且可以选择以 ASCII 字母 D 或 d 为后缀(第 4.2.3 节)。
Change your declaration to:
将您的声明更改为:
float density = 20.0f;
In general, consider why you're using float
- it has less precision than double
, and isn't used nearly as often.
一般而言,请考虑您使用的原因float
- 它的精度低于double
,并且几乎不经常使用。
回答by user207421
As per my understanding, float is used for decimal variables.
根据我的理解, float 用于十进制变量。
No. Numerical literals with fraction parts are treated as doubles by default.
不可以。默认情况下,带有分数部分的数字文字被视为双精度型。
回答by Devaashish Sharma
In Java by default decimal values are stored as double. But for storing it as a floating point number we explicitly need to declare, like
在 Java 中,默认十进制值存储为双精度值。但是为了将其存储为浮点数,我们需要明确声明,例如
float varName = 10.0f; or float varName = (float)10.0;
浮动变量名 = 10.0f; 或 float varName = (float)10.0;