java - Java order of operations clarification
问题描述
I am having trouble understanding order of operations in Java. Isn't arithmetic operator evaluated before relational. If so, why does this code run without error? Shouldn't y/z be evaluated first, causing an Arithmetic exception.
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 10;
int y = 20;
int z = 0;
if(x>y && 10 < y/z) {
System.out.print("Homer");
}
}
解决方案
&& 运算符短路:即:如果第一次检查失败,它不会打扰进行第二次检查。现在,如果 X 实际上大于 Y,则在执行第二次检查时会出现除以 0 异常。
出于好奇,我尝试直接输入 10/0 ,它仍然按顺序评估条件。我想这是有道理的,因为您不希望进行任何已经预先确定为错误的实际计算。只会浪费时间。
推荐阅读
- java - 如何从 ArrayList 中删除索引并打印已删除索引的总整数?
- flutter - 无效参数:URI 中未指定主机 - Image.network
- python - 生成回文的函数
- reactjs - 如何为 React 导航抽屉屏幕标题提供自定义样式
- getstream-io - Getstream 基于位置的提要?
- python - 如何使用for循环在python中反转字符串数字
- git - 如何在 Github 存储库上将主分支重命名为 master
- prestashop - Prestashop 1.7 如何编辑 $stylesheets smarty 变量
- apache-kafka - Apache Kafka:将 __consumer_offsets 重新分配给新代理时出现 kafka.common.OffsetsOutOfOrderException
- c# - 从用户那里收集 int 并添加到数组中,直到他们输入“OK”