java基础语法学习03

运算符

算数运算符

  • 加,减,乘,除,取余数,自增,自减

例一

package operator;

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

        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

    }
}

例二

类型转换

package operator;

public class Demo02 {
    public static void main(String[] args) {

        long a = 123123123123L;
        int  b =123;
        short c =10;
        byte d = 8;

        //运算的变量中有long类型,那么运算结果就是long类型
        
        System.out.println(a+b+c+d);  //long类型,输出123123123264
        System.out.println(b+c+d);    //int类型,输出141
        System.out.println(c+d);      //int类型,输出18
        //System.out.println((String)(c+d));

    }
}

自增自减

public class Demo04 {
    public static void main(String[] args) {

        //++ 自增  --自减   一元运算符
        int a = 3;

        int b = a++;//执行这句代码后,先将a赋值给b,再进行a的自增运算
        //a++  a=a+1
        int c = ++a;//执行这句代码前,先进行a的自增运算,再将a赋值给c
        //a++  a=a+1

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //幂运算 2^3  很多运算,会使用工具类来运算,例如 Math类工具
        double pow = Math.pow(2,3);
        System.out.println(pow);

    }
}

关系运算符

  • 大于,小于,大于等于,小于等于,相等,不等于
package operator;

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:布尔值 false true

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

        System.out.println(c%a);  //取余(模运算),c除以a后所得余数,输出结果1
        System.out.println(a%c);  //商为0时,余数就是除数,输出结果10

        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 operator;

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);
        //不会执行c++这个自增操作,因为&&左侧已经判断出d这个值为false,所以右侧的就不会去计算了,不会执行自增操作。

        System.out.println(d);
        System.out.println(c);

    }
}

输出结果

a&&b:false
a||b:true
!(a&&b):true
false
5

位运算符

  • & / ^ ~
package operator;

public class Demo06 {
    public static void main(String[] args) {

        //位运算
        /*
        A = 0011 1100
        B = 0000 1101
        ---------------------
        A&B = 0000 1100    //都为1,得1
        A/B = 0011 1101    //有1,得1
        A^B = 0011 0001    //相同为0,不同为1
        ~B  = 1111 0010    //非B,取反
        ---------------------
       
    }
}
  • << 左移 >>右移

    位数左移,左移一位乘以2
    位数右移,右移一位除以2

package operator;

public class Demo06 {
    public static void main(String[] args) {

        //位运算
        /*
        2*8=16,如何实现? 2*2*2*2=16
        利用位运算,效率极高。
        <<   位数左移,左移一位乘以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);////在2的基础上左移3位,相当于乘以8,结果为16
    }
}

拓展赋值运算符

  • += -= *= /=
package operator;

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

        a+=b;  //a=a+b
      
        System.out.println(a);//输出结果30
		
    }
}

以下

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

字符串连接符

package operator;

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

        //字符串连接符    +     String
        /* 在+号的左右两边如果有字符串,也就是String类型
        那么,其余的的都会被当作字符串来连接起来
        将字符串的双引号""(将字符串放在最后),放在最后进行运算
        或者将其余变量用括号包起来,进行提前运算
        可以避免变成字符串运算
         */

        //从左向右就行计算
        
        System.out.println(a+b);      //30
        System.out.println(""+a+b);   //存在字符串,于是将a和b看作字符串,结果为1020
        System.out.println(a+""+b);   //存在字符串,于是将a和b看作字符串,结果为1020
        System.out.println(a+b+"");   //字符串在连接符+的最后面,先进行加法运算,再连接,结果为30
        System.out.println(""+(a+b)); //括号包起来的a+b先进行运算,再连接,结果为30


    }
}

条件运算符

  • 三元运算符
  • x ? y : z 如果x==true,则结果为y,否则结果为z
package operator;

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-03-04 20:04  一口不多  阅读(52)  评论(0)    收藏  举报