看下面的三行代码.
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 LiteralsA 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.
如果没有明确的缩小转换,您不能将 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 f or F to denote a float value
- 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 ConversionAssignment 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 (ell);否则它是 int 类型.后缀 L 是首选,因为字母 l (ell) 通常很难与数字 1 区分开来(一个).
JLS 3.10.1 Integer LiteralsAn integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int. The suffix L is preferred, because the letter l (ell) is often hard to distinguish from the digit 1 (one).
更多推荐
在 Java 中表示浮点值
发布评论