在Java中表示浮点值 [英] Representing float values in Java
问题描述
float f = 1;
float g = 1.1;
float h = 1.1f;
第二行有编译错误,而其他行没有编译错误。第一行工作正常,没有后缀f,第三行是后缀f。为什么会这样?
Java中的浮点文字是 double
默认值。
JLS 3.10.2浮点文字
浮点文字是一种类型如果后缀为ASCII字母
F
或f
,则float
>;否则它的类型是double
,它可以选择后缀为ASCII字母D
或d
。
您不能指定 double
值赋予 float
,而不需要明确的缩小转换。因此,您有两个选项:
- 对于文字,使用后缀
f
或F
来表示一个float
值 - 对于非文字,转换为
(float)
后者的一个例子是:
double d = 1.1;
float f =(float)d; //编译好!
扩展转换
为什么这样编译:
float f = 1;
是因为从 int
到
$ b
float 可以隐式地在赋值语境中完成。 =http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.2 =noreferrer> JLS 5.2作业转换
赋值转换在将表达式的值分配给变量时发生:表达式的类型必须转换为变量的类型。赋值上下文允许使用以下之一:
$ b $ ul
扩展原始转换(§5.1.2)
[...]
JLS 5.1.2扩展原始转换
以下19个原始类型上的特定转换被称为扩展原始转换:
int code>至
long
,float
或double
- [b]
其他数据类型后缀为文字
如上所述,还有 D
或 d
后缀为 double
。例如:
static void f(int i){
System.out.println(( INT));
static void f(double d){
System.out.println((double));
}
// ...
f(1); //打印(int)
f(1D); //打印(double)
还有一个后缀 long
文字,即 L
或 l
(小写字母)。强烈建议您使用大写变体。
JLS 3.10.1 Integer Literals
如果后缀为ASCII字母
L
或<$ c,则整数字符串的类型为long
$ c> l (ell
);否则它是int
类型。后缀L
是首选,因为字母l
(ell
)通常很难与数字1
(一个
)区分开来。
Look at the three lines of code below.
float f = 1;
float g = 1.1;
float h = 1.1f;
Second line has compilation errors, while the other lines do not have compilation errors. First line is working fine without suffix f and third line is working with suffix f. Why is this?
Floating point literals in Java is a double
value by default.
JLS 3.10.2 Floating-Point Literals
A floating-point literal is of type
float
if it is suffixed with an ASCII letterF
orf
; otherwise its type isdouble
and it can optionally be suffixed with an ASCII letterD
ord
.
You can't assign a double
value to a float
without an explicit narrowing conversion. You therefore have two options:
- For literals, use the suffix
f
orF
to denote afloat
value - For non-literals, use an explicit cast
(float)
An example of the latter is:
double d = 1.1;
float f = (float) d; // compiles fine!
On widening conversions
The reason why this compiles:
float f = 1;
is because the widening conversion from int
to float
can be done implicitly in the context of an assignment.
JLS 5.2 Assignment Conversion
Assignment conversion occurs when the value of an expression is assigned to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following:
- a widening primitive conversion (§5.1.2)
- [...]
JLS 5.1.2 Widening Primitive Conversion
The following 19 specific conversions on primitive types are called the widening primitive conversions:
int
tolong
,float
, ordouble
- [...]
Other data type suffix for literals
As mentioned above, there's also the D
or d
suffix for double
. Consider this snippet for example:
static void f(int i) {
System.out.println("(int)");
}
static void f(double d) {
System.out.println("(double)");
}
//...
f(1); // prints "(int)"
f(1D); // prints "(double)"
There's also a suffix for long
literals, which is L
or l
(lowercase letter). It is highly recommended that you use the uppercase variant.
JLS 3.10.1 Integer Literals
An integer literal is of type
long
if it is suffixed with an ASCII letterL
orl
(ell
); otherwise it is of typeint
. The suffixL
is preferred, because the letterl
(ell
) is often hard to distinguish from the digit1
(one
).
这篇关于在Java中表示浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!