java - 我们会通过隐式转换来降低精度吗

标签 java implicit-conversion implicit

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/

相关文章:

java - 如何测试非法访问异常?

c++ - 涉及旧式枚举和整数类型的重载解析

c++ - 如果 T 可转换为 U,则将 class<T> 转换为 class<U>

scala - 如何为我的 Scala 类定义强制转换和隐式强制转换操作?

java - Jtree节点添加

java - 服务器始终使用 Jersey 2 + Tomcat 6 返回 404 错误

java - 导入未使用的包不好吗?

c++ - char 类型乘以另一个 char

Scala泛型类型和隐式类型类

scala - 类型类和带有单例类型/案例对象的隐式