基本运算符

基本运算符

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

赋值运算符:=

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

逻辑运算符:&& , || , !

位运算符:& , | , ^ , ~ , >> , << , >>>

条件运算符:? :

扩展赋值运算符:+= , -= , *= , /=

算术运算符

package text_for_operator;
//算术运算符
public class Demo01 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a+b);//30
        System.out.println(a-b);//-10
        System.out.println(a*b);//200
        System.out.println(a/(double)b);//0.5
        System.out.println(a%b);//10

        int c = a++;
        System.out.println(a);//11
        System.out.println(c);//10
        int d = ++a;
        System.out.println(d);//12
        System.out.println(a);//12
    }
}

package text_for_operator;
public class Demo02 {
    public static void main(String[] args) {
        long a = 1513423143;
        int b = 123;
        short c = 10;
        byte d = 8;

        //有long的运算结果都为long,其余都为int
        System.out.println(a+b+c+d);//Long
        System.out.println(b+c+d);//Int
    }
}

关系运算符

package text_for_operator;
//关系运算符
public class Demo03 {
    public static void main(String[] args) {
        int a = 20;
        int b = 30;
        System.out.println(a>b);//false
        System.out.println(a<b);//true
        System.out.println(a==b);//false
        System.out.println(a!=b);//true
    }
}

逻辑运算符

package text_for_operator;
//逻辑运算符
public class Demo04 {
    public static void main(String[] args) {
        //与(and &&)或(or ||)非(取反 !)
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:"+(a&&b));//false
        //当a和b都是true时为true,反之为false
        System.out.println("a || b:"+(a||b));//true
        //其中有一个为true时为true,反之为false
        System.out.println("!(a&&b):"+!(a&&b));//true
        //true时为false,false时为true
    }
}

位运算符

package text_for_operator;
//位运算符
public class Demo05 {
    public static void main(String[] args) {
        int a = 0011_1100;
        int b = 0000_1101;

        System.out.println(a&b);//0000_1100
        //a和b都为1时,则为1,反之为0
        System.out.println(a|b);//0011_1101
        //a和b其中一个为1时,则为1,反之为0
        System.out.println(a^b);//0011_0001
        //a和b相同时,则为0,反之为1
        System.out.println(~b);//1111_0010
        //取反    1时为0,0时为1

        int c = 3;
        System.out.println(c<<3);//24
        System.out.println(c>>3);//0.375
        System.out.println(c>>>1);//1

        double pow = Math.pow(4, 2);//4^2
        System.out.println(pow);//16

    }
}

赋值运算符

package text_for_operator;
//赋值运算符
public class Demo06 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

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

        System.out.println(a);//30
        System.out.println(b);//-10

        //字符串连接符    +   ,String
        System.out.println(a+b);//20
        System.out.println(""+a+b);//30-10
        System.out.println(a+b+"");//20
        System.out.println(""+(a+b));//20
        //括号决定优先级

    }
}

条件运算符

package text_for_operator;
//条件运算符
public class Demo07 {
    public static void main(String[] args) {
        //x?y:z
        //如果x==true,则结果为y,否则结果为z
        String fact = "我去了顺职院";
        String type = fact == "我去了清华"?"牛逼":"呃";
        System.out.println(type);//呃
    }
}
posted @ 2022-07-15 01:01  YlMXY  阅读(130)  评论(0)    收藏  举报