&&和||都属于短路运算,若第一个表达式能够确定整个表达式的值,第二个表达式不会被运算。以下两段ifelse的效果是一样的。
----------------------------
public class Test {
public static void main(String[] args) {
int x = 1;
double y = 5.6;
// 第一段ifelse
if (x != 0) {
if (y / x > x - y) {
System.out.println("Yeah!");
} else {
System.out.println("No!");
}
} else{
System.out.println("No!");
}
// 第二段ifelse
if (x != 0 && y / x > x - y) {
System.out.println("Yeah!");
} else {
System.out.println("No!");
}
}
}