Java历届试卷中有一道题让我兄弟感慨:
With implicit conversion of primitive data types, you can lose precision and get incorrect results.
A True, B False
The key to the answer is A: True
我认为它既不会失去精度也不会得到不正确的结果。我知道显式转换可能会失去精度并得到不正确的结果,但隐式转换不会。
例如:
int i = 9;
short s = 3;
i = s; // implicit conversion, neither loose
//precision nor incorrect results
s = i; // compile error, do we call this implicit conversion?
//if yes, then the answer to question 3 is True,
//but I don't think this is an implicit conversion,
//so I think answer is false.
如注释中所述:
Implicit type conversion: The programmer does not make any attempt to convert the type, rather the type is automatically converted by the system under certain circumstances.
谁能给个建议?
非常感谢。
最佳答案
答案 = A
float f = Long.MAX_VALUE;
System.out.println(Long.MAX_VALUE);
System.out.printf("%.0f", f);
输出
9223372036854775807
9223372036854776000
关于java - 我们会通过隐式转换来降低精度吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19479268/