运算符

运算符

算术运算符

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

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        //Ctrl + D:复制当前行到下一行
        int a = 10;
        int b = 20;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);
        System.out.println(a/(double)b);

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

        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
        //式子中有long类型的,结果类型也为long
        //式子中没有long类型的,结果类型都为int
    }
}

关系运算符

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

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回结果:正确/错误,通过布尔值表示

        int a = 10;
        int b = 20;
        int c = 21;

        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        System.out.println(c%a);
    }
}

自增、自减

自增++,自减--

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

        //幂运算,很多运算我们会使用一些工具类来操作
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}

逻辑运算符

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

public class Demo05 {
    //逻辑运算符
    public static void main(String[] args) {
        //或 且 非
        boolean a = true;
        boolean b = false;

        System.out.println("a&&b:"+(a&&b));
        System.out.println("a||b:"+(a||b));
        System.out.println("!(a&&b):"+!(a&&b));

        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);
        System.out.println(d);
        System.out.println(c);


    }
}

位运算符

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

public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101
        --------------------
        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001
        ~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);
        System.out.println(16>>3);
    }
}

扩展赋值运算符

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

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a+=b;
        System.out.println(a);
        a-=b;
        System.out.println(a);

        /*
        字符串连接符  +  ,两边只要有一边出现字符串类型,
        就会把另一个操作数或其他操作数也转换为String类型再连接
         */
        System.out.println(a+b);
        System.out.println("String"+a+b);
        System.out.println(a+b+"String");
        /*
        若字符串在前面,则后面的会进行拼接
        若字符串在后面,前面的会先进行运算再拼接
         */

    }
}

三元运算符

public class Demo08 {
    public static void main(String[] args) {
        //三元运算符
        //x ? y : z
        //如果x==true,则结果为y,否则结果为z

        int score = 80;
        String type = score<60?"不及格":"及格";
        System.out.println(type);
    }
}
posted @ 2021-08-05 17:31  浑身充满希望  阅读(57)  评论(0)    收藏  举报