zoukankan      html  css  js  c++  java
  • 与或

    
    
    package com.lgw.aha;
    
    public class HuoAndYu {
        public static void main(String[] args) {
            
        System.out.println(true & false);//false
        System.out.println(true & true);//true
        System.out.println(false & false);//false
        System.out.println(false & true);//false
        
        System.out.println(true && false);//false
        System.out.println(true && true);//true
        System.out.println(false && false);//false
        System.out.println(false && true);//false
        
        /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
         * &--->与    &&--->短路与
         * 由上黄色警告可知:短路与-当左边表达式为false时右边已经不执行了,所以IDE有标识(图1)
         */
        
        System.out.println(true | false);//true
        System.out.println(true | true);//true
        System.out.println(false | false);//false
        System.out.println(false | true);//true
        
        System.out.println(true || false);//true
        System.out.println(true || true);//true
        System.out.println(false || false);//false
        System.out.println(false || true);//true
        
        /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
         * |--->或    ||--->短路或
         * 由上黄色警告可知:短路或-当左边表达式为true时右边已经不执行了,所以IDE有标识(图2)
         */
        
        
        /*======>>>短路和不短路的区别<<<======
         *1. 不短路的与和或:将运算符左右两边的表达式都进行运算得出布尔值后再进行运算
         *2. 短路与:若左边表达式为false则不会对右边表达式判断,因为只要出现一个false则结果必是false
         *3. 短路或:若左边表达式为true则不会对右边表达式判断,因为只要出现一个true则结果必是true
         *4. 由上述可知:短路的与和或执行效率要高
         * 
         * 或--->5次机会只要有一次考试得了60分就算及格,每次考试之间的关系就是或
         * 与--->5次机会必须每次考试都得了60分才算及格,每次考试之间的关系就是与
         */
    
    //  上述第4点举例
    
        int a = 1 ; int b = 10;
        System.out.println(false & a++==1);//左边为false,右边的表达式继续执行,即a自增1
        System.out.println(a);//2
    
        a= 1;
        System.out.println(false && a++==1);//左边的表达式为false,右边的表达式a++将不会执行
        System.out.println(a);//1
    
        System.out.println(true | b++==1);//左边为false,右边的表达式继续执行,即b自增1
        System.out.println(b);//11
    
        b= 10;
        System.out.println(true || b++==1);//左边的表达式为false,右边的表达式b++将不会执行
        System.out.println(b);//10
        
        /*
         * 上述  a++ == 1  和  b++  == 1 只是得出一个布尔型值
         */
        }
    }
     

    图1

     (图1)

    图2

    (图2)

    作者:stuka

    个性签名:教育的目的是为了获得与个人兴趣爱好相适应并得到国家和社会认可的能力

    如有帮助请点击右下“推荐”

    wechat alipay

  • 相关阅读:
    eclipse中的Invalid text string (xxx).
    在jsp文件中出现Unknown tag (c:out)
    eclipse 界面复原
    ecilpse 纠错插件
    Multiple annotations found at this line:- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    Port 8080 required by Tomcat v9.0 Server at localhost is already in use. The server may already be running in another process, or a system process may be using the port.
    调用第三方https接口
    调用第三方http接口
    创建带值枚举
    spring整合redis之Redis配置文件
  • 原文地址:https://www.cnblogs.com/stuka/p/9554417.html
Copyright © 2011-2022 走看看