Day06 java operator

java operator

Increment and Decrement Operator

public class Demo02 {
    
    
    public static void main(String[] args) {
    
    
        int a = 3;
        
        int b = a++; //执行完这行代码时,先给b赋值,然后a自增
        /*相当于 b = a;
                a = a + 1;
                此时a = 4
                */
        
        int c = ++a;//执行这行代码时,a先自增,然后给c赋值
        /*相当于 a = a + 1;
                c = a;
                此时a = c =5;
         */
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        //输出结果为:5,3,5
        
    }
}

The decrement operator is the same as the increment

Bit operator

And or not

public class Demo02 {
    
    
    public static void main(String[] args) {
    
    
        /* 假设有两个数
        A = 0011 1100
        B = 0000 1101
        A&B(A与B) = 0000 1100
        A|B(A或B) = 0011 1101 
        A^B(异或) = 0011 0001 //相同为0,不相同为1
        ~B(非) = 1111 0010 
        */
        
    }
}

Move left move right

面试题:2*8怎样运算更快?
在计算机位运算里面,2*2*2*2更快,位运算效率更高
'<<':左移
'>>':右移
Sysyem.out.println(2<<3);
//结果为:16
/*
    0000 0000  0
    0000 0001  1
    0000 0010  2  
    0000 0011  3
    0000 0100  4
    0000 1000  8
    0001 0000 16
    对于2来说,其中的1向左移动一位,其值*2
                    向右移动一位,其值/2
     所以(2<<3)的值为16
     */

Ternary operator

Ternary operator: ? :

x ? y : z

If x==true, the result is y, otherwise the result is z

public class Demo02 {
    
    
    public static void main(String[] args) {
    
    
        int score = 61;
        String type = null;
        type = score < 60 ? "不及格":"及格";
        System.out.println(type);
    }
}
//输出结果为'及格'!!!

Guess you like

Origin blog.csdn.net/weixin_48804712/article/details/109296709
Recommended