原始类型转换主要有两种:
- 扩大转化;
- 缩小转化范围。
我正在 Java SE Specification 中阅读有关扩大转换的内容,我在这里看到了以下图片:
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/