Is it valid to compare a double with an int in java?

后端 未结 6 1403
暗喜
暗喜 2020-11-28 10:41
Utilities.getDistance(uni, enemyuni) <= uni.getAttackRange()

Utilities.getDistance returns double and getAttackRange returns int. The above code

6条回答
  •  死守一世寂寞
    2020-11-28 11:05

    Yes, it's valid - it will promote the int to a double before performing the comparison.

    See JLS section 15.20.1 (Numerical Comparison Operators) which links to JLS section 5.6.2 (Binary Numeric Promotion).

    From the latter:

    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.

    • ...

提交回复
热议问题