由于第一部分的计算结果为 false,此代码是否不会计算 if 语句的除以零部分?如果是这样,那么对于所有 Java IDE 中的所有情况都是如此吗?或者某些编译器会抛出异常?
int n = 0;
int x = 5;
if (n != 0 && x / n > 100) {
System.out.println(" s1");
} else {
System.out.println("s2");
}
最佳答案
来自JLS §15.23 :
The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.
所以不,你不会遇到异常(exception)。
显然,这假设您有一个单线程或线程安全的环境 - 如果您在混合中引入可见性问题,那么任何人都可以猜测。
关于java - 这个 if 语句会抛出异常还是忽略它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29802050/