Is it valid to compare a double with an int in java?(在java中将double与int进行比较是否有效?)
问题描述
Utilities.getDistance(uni, enemyuni) <= uni.getAttackRange()
Utilities.getDistance 返回 double 并且 getAttackRange 返回 int.上面的代码是 if 语句的一部分,它必须是真的.那么比较有效吗?
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:
如果任一操作数是 double 类型,则另一个操作数将转换为 double.
If either operand is of type double, the other is converted to double.
...
这篇关于在java中将double与int进行比较是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在java中将double与int进行比较是否有效?
基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
