java - Java规范示例中的错误?

标签 java type-conversion primitive

原始类型转换主要有两种:

  • 扩大转化;
  • 缩小转化范围。

我正在 Java SE Specification 中阅读有关扩大转换的内容,我在这里看到了以下图片:

enter image description here

int big = 1234567890;
float approx = big; // good, it's widening conversion (int -> float)
System.out.println(big - (int)approx); // what? (float -> int)

在最后一行代码中,我认为它是narrowing 转换,对吗?如果我错了,请解释我!提前致谢。

最佳答案

是的,最后 行使用了缩小转换,这就是为什么需要强制转换的原因。例子用来演示中线加宽转换丢失信息,仅此而已:

This program prints -46 thus indicating that information was lost during the conversion from type int to type float because values of type float are not precise to nine significant digits.

关于java - Java规范示例中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31144486/

相关文章:

java - 类型不匹配 : cannot convert from double[][] to double[]

java - 如何将托管 bean 注入(inject) FacesContext?

python - 日期时间变量为空

c++ - 如何将字节打印为十六进制?

XNA/MonoGame : depth sorting alpha blended UserPrimitives

c# - int (Int32) 是否被视为 .NET 中的对象或原语(不是 int?)?

java - 解密java时出错

java - 使用循环将字符串存储在列表中,然后打印列表

java - 从字节转换为字符串的任何其他逻辑

xcode - 将原语传递到OCMock的 stub