Wrong type in Java conditional assignment

喜夏-厌秋 提交于 2019-11-30 06:17:27

Well, that is because of the JLS specs for the conditional operator:

Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:

  • ...
  • Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.

Numeric promotion is defined here in §5.6.2. It says:

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

  • If either operand is of type double, the other is converted to double.
  • ...

Well 0.0 is still == to 0

System.out.println(0 == 0.0); // equals true

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!