java常用逻辑运算符

运算符

短路与(&&)

  • 两个变量都为真,结果才为true
  • 只要第一个变量为假,结果为false,后面的运算不再进行

短路或(||)

  • 只要有一个变量为真,结果就为true
  • 只要第一个变量为真,结果为true,后面的运算不再进行

位运算

public class Demo01{
    
    
    public static void main(String[] args){
    
    
        /*
        A = 0011 1100
        B = 0000 1101
        -------------------------
        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001//相同为0不同为1
        ~B = 1111 0010
        
        2*8 =16 2*2*2*2=16
        << 等于*2
        >> 等于/2
        
        0000 0000	0
        0000 0001	1
        0000 0010	2
        0000 0011	3
        0000 0100	4
        0000 1000	8
        0001 0000	16
        */
        System.out.println(2<<3);//16
    }
}

二元运算符

    private static void f3() {
    
    
        int a=10;
        int b=20;
        a+=b;
        a-=b;
        System.out.println(a+b);//30
        //字符串连接   +    ,String类型
        System.out.println(""+a+b);//1024,当成字符串了,如果要做加法就给a+b加一个括号
        System.out.println(""+(a+b));//30
        System.out.println(a+b+"");//30
    }

猜你喜欢

转载自blog.csdn.net/Werdio/article/details/112856578
今日推荐