欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java && & || |,

来源: javaer 分享于  点击 5108 次 点评:10

Java && & || |,


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 ||.

转载于:https://www.jianshu.com/p/388cfb3661d8

相关文章

    暂无相关文章
相关栏目:

用户点评