Java

Java && & || |

2018-08-30  本文已影响4人  JaedenKil
public class Operator02Demo {
    public static void main(String[] args) {
        System.out.println(test());
    }
    static boolean test() {
        return true && 1 / 0 != 1;
    }
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at OperationDemo.test(OperationDemo.java:6)
    at OperationDemo.main(OperationDemo.java:3)

public class Operator02Demo {
    public static void main(String[] args) {
        System.out.println(test());
    }
    static boolean test() {
        return true & 1 / 0 != 1;
    }
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Operator02Demo.test(Operator02Demo.java:6)
    at Operator02Demo.main(Operator02Demo.java:3)

public class Operator02Demo {
    public static void main(String[] args) {
        System.out.println(test());
    }
    static boolean test() {
        return false && 1 / 0 != 1;
    }
}
false

public class Operator02Demo {
    public static void main(String[] args) {
        System.out.println(test());
    }
    static boolean test() {
        return false & 1 / 0 != 1;
    }
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Operator02Demo.test(Operator02Demo.java:6)
    at Operator02Demo.main(Operator02Demo.java:3)

& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false

public class Operator02Demo {
    public static void main(String[] args) {
        System.out.println(test());
    }
    static boolean test() {
        return true || 1 / 0 != 1;
    }
}
true

public class Operator02Demo {
    public static void main(String[] args) {
        System.out.println(test());
    }
    static boolean test() {
        return true | 1 / 0 != 1;
    }
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Operator02Demo.test(Operator02Demo.java:6)
    at Operator02Demo.main(Operator02Demo.java:3)

public class Operator02Demo {
    public static void main(String[] args) {
        System.out.println(test());
    }
    static boolean test() {
        return false || 1 / 0 != 1;
    }
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Operator02Demo.test(Operator02Demo.java:6)
    at Operator02Demo.main(Operator02Demo.java:3)

public class Operator02Demo {
    public static void main(String[] args) {
        System.out.println(test());
    }
    static boolean test() {
        return false | 1 / 0 != 1;
    }
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Operator02Demo.test(Operator02Demo.java:6)
    at Operator02Demo.main(Operator02Demo.java:3)

exprA | exprB <-- this means evaluate exprA then evaluate exprB then do the |.
exprA || exprB <-- this means evaluate exprA and only if this is false then evaluate exprB and do the ||.

上一篇 下一篇

猜你喜欢

热点阅读