Java运算符

运算符

  • 算术运算符:+, -, *, /, %, ++, --
  • 赋值运算符:=
  • 关系运算符:<, >, <=, >=, ==, !=, instanceof
  • 逻辑运算符:&&, ||, !
  • 位运算符:&, |, ^, >>, <<, >>>
  • 条件运算符:? :
  • 扩展赋值运算符:+=, -=, *=, /=

算术运算符:

public class Demo002 {
    public static void main(String[] args) {
        long a = 13243243245254535L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a+b+c+d);//13243243245254676   Long类型 如果有long,结果就为long类型;同理如果有double,结果就为double类型
        System.out.println(b+c+d);//141  Int类型  如果没有long,结果就为int类型,不管有没有int类型相加
        System.out.println(c+d);//18  Int类型
    }
}

关系运算符:

public class Demo003 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确,错误  布尔值
        //if
        int a = 10;
        int b = 20;
        int c = 21;

        //取余:模运算
        System.out.println(c%a);//1    c / a   21 / 10 = 2....1

        System.out.println(a>b);//false
        System.out.println(a<b);//true
        System.out.println(a==b);//false
        System.out.println(a!=b);//true
    }
}

++ -- :自增 自减

public class Demo004 {
    public static void main(String[] args) {
        //++  --   自增,自减
        int a = 3;
        int b = a++;//执行完这行代码后,先给b赋值,再自增*************************************
        //a++  a = a + 1(隐藏代码)
        System.out.println(a);//4
        //++a  a = a + 1(隐藏代码)
        int c = ++a;//执行这行代码前,先自增,再给c赋值****************************************

        System.out.println(a);//5
        System.out.println(b);//3
        System.out.println(c);//5

        //幂运算 2^3  2*2*2 = 8   java里面没有^,很多运算,我们会使用很多工具类来操作!
        double pow = Math.pow(2, 3);
        System.out.println(pow);//8.0
    }
}

逻辑运算符:

//逻辑运算符
public class Demo005 {
    public static void main(String[] args) {
        //与(and)  或(or)  非(去反)
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:"+(a && b));//逻辑与运算:两个变量都为真,结果才为true
        System.out.println("a || b:"+(a || b));//逻辑或运算:两个变量有一个为真,结果就为true
        System.out.println("!(a && b):"+(!(a && b)));//如果是真,则为false,如果是假,则为true

        //短路运算
        int c = 5;
        boolean d = (c<4) && (c++<4);//c<4肯定为false,所以后边不执行,所以c的值依然为5
        System.out.println(c);//5
        System.out.println(d);//false
    }
}

位运算符:

public class Demo006 {
    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
        效率极高!
        <<(左移)相当于*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
    }
}

字符串连接符 +:

public class Demo007 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        a+=b;//a = a+b
        a-=b;//a = a-b

        System.out.println(a);//10

        //字符串连接符  +    ,String类型""
        System.out.println(a+b);//30
        System.out.println(""+a+b);//1020    字符串在前面,后面的拼接******************************
        System.out.println(a+b+"");//30      字符串在后面,前面的依旧运算
    }
}

条件运算符:

public class Demo008 {
    public static void main(String[] args) {
        // x ? y : z
        //如果x==true,则结果为y,否则结果为z

        int score = 50;
        String type = score < 60 ? "不及格" : "及格";//必须掌握
        // if
        System.out.println(type);//不及格
    }
}
posted @ 2021-01-12 11:28  llcodee  阅读(55)  评论(0)    收藏  举报
//Markdown复制代码按钮 //Markdown代码行号