Java的运算符基础知识与扩展

16.运算符

  • 算术运算符 +,-,*,/,%,++,–
  • 赋值运算符 =
  • 关系运算符(比较运算符)>, >=, <=, <, ==, != ,instancof
  • 逻辑运算符 &&, ||, !
  • 位运算符 & , | , ^ , ~, >> , <<, >>> (了解!!)
  • 条件运算符 ?:
  • 扩展赋值运算符 += , -= ,*= , /=
package operator;

public class Demo1 {
    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 / b);      //0  存在精度问题需要进行强制转换
        System.out.println(a / (double) b);  //0.5

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


    }
}

package operator;

public class Demo2 {
    public static void main(String[] args) {
        long a = 132456468735L;
        int b = 520;
        short c = 120;
        byte d = 20;

        System.out.println(a + b + c + d); //Long  如果参与运算的整数种有long则最终结果为long
        System.out.println(b + c + d); //Int 否则其他没有long的整数相加,结果都为int
        System.out.println(c + d); //Int
    }
}

  • 其他没有long的整数相加,结果都为int

关系运算符

package operator;

public class Demo3 {
    public static void main(String[] args) {
//        关系运算符 返回值是一个布尔值 正确,错误
        int a = 10;
        int b = 10;
        int c = 20;

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

    }
}

一元运算符

package operator;

public class Demo4 {
    public static void main(String[] args) {
//        一元运算符  ++ ,--
        int a = 10;
        int b = a++;  // ++a ---> 先赋值再自增  b = a++; 相当于 b = a; a = a + 1
        int c = ++a;  // ++a ---> 先自增再赋值  b = ++a;  相当于 a = a + 1; b = a;

        System.out.println(a);  //12
        System.out.println(b);  //10
        System.out.println(c);  //12
    }
}

逻辑运算符

package operator;

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

//        逻辑与&&   逻辑或||  逻辑非!
       boolean a = true;
       boolean b = false;

       System.out.println("a&&b:" + (a && b));      //false  一假则假
       System.out.println("a||b:" + (a || b));      //true   一真则真
       System.out.println("!(a&&b):" + (!(a && b)));//true   真假取反


//        短路运算
       int i = 4;
       boolean j = 4 < 3 && i++ > 5;  // 如果前边的条件已经满足了判断,则不会进行后边的运算
       System.out.println(j);  // false
       System.out.println(i);  // 4
   }
}

位运算

package operator;

public class Demo6 {
    public static void main(String[] args) {
        /*
         * &与,|或,~非,^异或
         *
         * A = 0000 1010
         * B = 1100 0011
         *
         * A&B = 0000 0010
         * A|B = 1100 1011
         * A^B = 1100 1001
         * ~A = 1111 0101
         *
         *    <<左移 *2  (移多少位乘多少个2)
         *    >>右移 /2  (移多少位除以多少个2,不足时高位补0)
         *
         *   效率极高!!
         *   0000 0000   -->0
         *   0000 0001   -->1
         *   0000 0010   -->2
         *
         * */

        System.out.println(2 << 3);  //16
        System.out.println(4 >> 3);  //0
    }
}

赋值运算符与字符串连接符

package operator;

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

        b += a;  // 相当于 b = b + a
        System.out.println(b);  //30

//      字符串连接符 +  当 + 号前一个变量为字符串时,+ 号为字符串连接符,会进行一个字符串的拼接操作,最终结果为一个String
        System.out.println("" + a + b);  //1030

    }
}

条件运算符与三元表达式

package operator;

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

        int score = 80;
        int score2 = 59;

//      三元表达式 x ? y : z   --> 如果x为真,结果为y,如果x为假,结果为z

        String ret1 = score > 60 ? "及格" : "不及格";
        String ret2 = score2 > 60 ? "及格" : "不及格";

        System.out.println(ret1);  //及格
        System.out.println(ret2);  //不及格

    }
}

运算符优先级

  • ()
  • 一元运算符(最高)
  • 算术运算符
  • 不需要可以去记忆优先级,必要时可以通过 ( ) 来改变优先级顺序,并不影响性能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pointer-faker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值