运算符

运算符

永远不要小看坚持的力量

算术运算符:+ - * / % ++ --

赋值运算符:=

关系运算符:> < >= <= == != instanceof

逻辑运算符:&& || !

位运算符(了解):& | ^ ~ >> << >>>

条件运算符(为了简化):? :

扩展赋值运算符(为了简化):+= -= *= /=

注意点

    public static void main(String[] args) {
        // 多个整数相加如果里面有long,结果为long,否则结果为int
        // 多个浮点数相加如果里面有double,结果为double,否则结果为float
        long a = 1234567890123L;
        int b = 150;
        short c = 24;
        byte d = 10;

        System.out.println(a + b + c + d); //long
        System.out.println(b + c + d); //int
        System.out.println(c + d); //int
        
        // ++详解 --原理相同    
        int e = 3;
        int f = e++; // 先将e的值赋值给f ,e再自增
        System.out.println(e);
        int g = ++e; // e先自增,再将e的值赋值给g
        System.out.println(e); //5
        System.out.println(f); //3
        System.out.println(g); //5
    }

位运算详解

// 位运算符 & | ^ ~
int i = 0b00111100;
int j = 0b00100111;

System.out.println(Integer.toBinaryString(i & j)); // 00100100
System.out.println(Integer.toBinaryString(i | j)); // 00111111
System.out.println(Integer.toBinaryString(i ^ j)); // 00011011 异或理解成两个数相加不进位
System.out.println(Integer.toBinaryString(~j)); // 11011000

// 2*8 = 2*2*2*2
System.out.println(1 << 4);

加号扩展

// +扩展
System.out.println(2 + 3); //5
System.out.println("abc" + "abc"); //abcabc
System.out.println("" + 2 + 3); //23
System.out.println(2 + 3 + ""); //5
posted @ 2021-10-16 12:11  Oh,mydream!  阅读(24)  评论(0编辑  收藏  举报