在 Java 中表示浮点值

时间:2023-01-16
本文介绍了在 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 字母 Ff 为后缀,则为 float 类型;否则它的类型是 double 并且可以选择以 ASCII 字母 Dd 为后缀.

JLS 3.10.2 Floating-Point Literals

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.

如果没有明确的缩小转换,您不能将 double 值分配给 float.因此,您有两种选择:

You can't assign a double value to a float without an explicit narrowing conversion. You therefore have two options:

  • 对于文字,使用后缀 fF 来表示 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;

是因为从 intfloat 的扩大转换可以在赋值的上下文中隐式完成.

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:

  • intlongfloatdouble
  • [...]


文字的其他数据类型后缀

如上所述,double 也有 Dd 后缀.以这个片段为例:


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 文字还有一个后缀,即 Ll(小写字母).强烈建议您使用大写变体.

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 字母 Ll (elllong>);否则它是 int 类型.后缀 L 是首选,因为字母 l (ell) 通常很难与数字 1 区分开来(一个).

JLS 3.10.1 Integer Literals

An 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 中表示浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何将 IEEE-11073 16 位 SFLOAT 转换为 Java 中的简单浮 下一篇:Android FTP 库

相关文章

最新文章