美文网首页Java
Java && & || |

Java && & || |

作者: JaedenKil | 来源:发表于2018-08-30 16:03 被阅读4次
    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 ||.

    相关文章

      网友评论

        本文标题:Java && & || |

        本文链接:https://www.haomeiwen.com/subject/imcdwftx.html