在java中比较double和int是否有效? [英] Is it valid to compare a double with an int in java?

查看:1032
本文介绍了在java中比较double和int是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Utilities.getDistance(uni, enemyuni) <= uni.getAttackRange()

Utilities.getDistance返回double,getAttackRange返回int。上面的代码是if语句的一部分,它需要是true。那么比较是否有效?

Utilities.getDistance returns double and getAttackRange returns int. The above code is part of an if statement and it needs to be true. So is the comparison valid?

谢谢

推荐答案

有效 - 它将在执行比较之前将 int 提升为 double

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

请参见 JLS第15.20.1节(数值比较运算符),其链接到 JLS第5.6.2节(二进制数值推广)

从后者:


扩展原始转换(§5.1.2)适用于转换以下规则中指定的一个或两个操作数:

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


    <

...

这篇关于在java中比较double和int是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆