JAVA中的|、||、&和&&

1.前言

  • 最近遇到了使用|||&&&的场景,有点模糊不清,现在这里书写下来记录。

2.先上代码

public class andOrTest {
    static int a = 0;
    static int b = 0;

    public static void main(String arg[]) {
        //在这里进行配置
        doubleOr();

    }

    private static void printlnAndZero() {
        System.out.println("a:" + a + "," + "b:" + b);
        a = 0;
        b = 0;
    }

    private static void justPrintLn() {
        System.out.println("a:" + a + "," + "b:" + b);
    }

    private static void zero() {
        a = 0;
        b = 0;
    }

    private static void justAnd() {
        // true false
        if (((a = 1) > 0) & ((b = -1) > 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // true true
        if (((a = 1) > 0) & ((b = -1) < 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // false false
        if (((a = 1) < 0) & ((b = -1) > 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // false true
        if (((a = 1) < 0) & ((b = -1) < 0)) {
            justPrintLn();
        }
        printlnAndZero();
        
    }

    private static void justOr() {
        // true false
        if (((a = 1) > 0) | ((b = -1) > 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // true true
        if (((a = 1) > 0) | ((b = -1) < 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // false false
        if (((a = 1) < 0) | ((b = -1) > 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // false true
        if (((a = 1) < 0) | ((b = -1) < 0)) {
            justPrintLn();
        }
        printlnAndZero();
        zero();
    }

    private static void doubleAnd() {
        // true false
        if (((a = 1) > 0) && ((b = -1) > 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // true true
        if (((a = 1) > 0) && ((b = -1) < 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // false false
        if (((a = 1) < 0) && ((b = -1) > 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // false true
        if (((a = 1) < 0) && ((b = -1) < 0)) {
            justPrintLn();
        }
        printlnAndZero();
        zero();
    }

    private static void doubleOr() {
        // true false
        if (((a = 1) > 0) || ((b = -1) > 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // true true
        if (((a = 1) > 0) || ((b = -1) < 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // false false
        if (((a = 1) < 0) || ((b = -1) > 0)) {
            justPrintLn();
        }
        printlnAndZero();
        // false true
        if (((a = 1) < 0) || ((b = -1) < 0)) {
            justPrintLn();
        }
        printlnAndZero();
        zero();
    }
}

3.输出结果

3.1 &

justAnd()
a:1,b:-1
a:1,b:-1
a:1,b:-1//这次是全为true的输出
a:1,b:-1
a:1,b:-1

3.2 |

jsutOr()
a:1,b:-1
a:1,b:-1//true false时的输出
a:1,b:-1
a:1,b:-1//true true时的输出
a:1,b:-1
a:1,b:-1
a:1,b:-1//false true时的输出

3.3 &&

doubleAnd()
a:1,b:-1
a:1,b:-1
a:1,b:-1//全为true的输出
a:1,b:0//当前面的判断为false时就不执行后面的操作
a:1,b:0//当前面的判断为false时就不执行后面的操作

3.4 ||

doubleOr()
a:1,b:0//当前面的判断为true时就不执行后面的操作
a:1,b:0//true false 的输出
a:1,b:0//当前面的判断为true时就不执行后面的操作
a:1,b:0//true true 的输出
a:1,b:-1
a:1,b:-1
a:1,b:-1//false true的输出

4.总结

  • & | 是逻辑与 、逻辑或操作,即先确定左右的布尔值,然后再做判断。
  • && ||是短路与、短路或操作,即先确定左边的布尔值,如果可以通过,就不确定右边的布尔值。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这东西记了又忘,忘了又记equals本来实现就是用的==,但是string比较特殊,有一个常量池1.string类...
    scoot929阅读 286评论 0 0
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,686评论 25 708
  • 给你明明哦。
    我想创业阅读 133评论 0 0
  • 手机上的时间每过一分钟,数字显示就向上翻动一下,就像翻动着我的生命。感谢那些生命中美好的人,即使有一天到了生命的尽...
    流砂阻水阅读 396评论 0 3
  • 这章讲述的是16世纪初的日耳曼。日耳曼相对于佛罗伦萨处于北方地区。对于文艺复兴的发生所带来的潮流,日耳曼地区也想追...
    沈惜朝阅读 465评论 0 0