Representing float values in Java(在 Java 中表示浮点值)
问题描述
看下面的三行代码.
float f = 1;
float g = 1.1;
float h = 1.1f;
第二行有编译错误,而其他行没有编译错误.第一行在没有后缀 f 的情况下工作正常,第三行在后缀 f 下工作.这是为什么呢?
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?
推荐答案
Java 中的浮点字面量默认是 double 值.
Floating point literals in Java is a double value by default.
如果浮点文字以 ASCII 字母 F 或 f 为后缀,则为 float 类型;否则它的类型是 double 并且可以选择以 ASCII 字母 D 或 d 为后缀.
JLS 3.10.2 Floating-Point Literals
A floating-point literal is of type
floatif it is suffixed with an ASCII letterForf; otherwise its type isdoubleand it can optionally be suffixed with an ASCII letterDord.
如果没有明确的缩小转换,您不能将 double 值分配给 float.因此,您有两种选择:
You can't assign a double value to a float without an explicit narrowing conversion. You therefore have two options:
- 对于文字,使用后缀
f或F来表示float值 - 对于非文字,使用显式转换
(float)
- For literals, use the suffix
forFto denote afloatvalue - For non-literals, use an explicit cast
(float)
后者的一个例子是:
double d = 1.1;
float f = (float) d; // compiles fine!
关于扩大转化率
这样编译的原因:
On widening conversions
The reason why this compiles:
float f = 1;
是因为从 int 到 float 的扩大转换可以在赋值的上下文中隐式完成.
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:
- 扩大的原始转换(§5.1.2)
- [...]
以下 19 种基本类型的特定转换称为加宽基本类型转换:
The following 19 specific conversions on primitive types are called the widening primitive conversions:
int到long、float或double- [...]
文字的其他数据类型后缀
如上所述,double 也有 D 或 d 后缀.以这个片段为例:
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)"
long 文字还有一个后缀,即 L 或 l(小写字母).强烈建议您使用大写变体.
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.
如果整数文字以 ASCII 字母 L 或 l (elllong>);否则它是 int 类型.后缀 L 是首选,因为字母 l (ell) 通常很难与数字 1 区分开来(一个).
JLS 3.10.1 Integer Literals
An integer literal is of type
longif it is suffixed with an ASCII letterLorl(ell); otherwise it is of typeint. The suffixLis preferred, because the letterl(ell) is often hard to distinguish from the digit1(one).
这篇关于在 Java 中表示浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中表示浮点值
基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
