Java自学笔记——基础语法(四)运算符

Java基础语法目录

Java自学笔记——基础语法(一)注释、标识符、关键字
Java自学笔记——基础语法(二)数据类型、类型转换
Java自学笔记——基础语法(三)变量、常量
Java自学笔记——基础语法(四)运算符
Java自学笔记——基础语法(五)包机制、JavaDoc

Java语言支持如下运算符:

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

一、运算符优先级排列

优先级运算符简介
1[ ] . ( )方法调用,属性获取
2!、~、 ++、 –一元运算符
3* 、/ 、%乘、除、取模(余数)
4+ 、 -加减法
5<<、 >>、 >>>左位移、右位移、无符号右移
6< 、<= 、>、 >=、 instanceof小于、小于等于、大于、大于等于,
对象类型判断是否属于同类型
7== 、!=2个值是否相等,2个值是否不等于。下面有详细的解释
8&按位与
9^按位异或
10|按位或
11&&
12||
13? :条件运算符
14=、 += 、-= 、*= 、/=、 %=、 &=、 |=、 ^=、 <、<= 、>、>= 、>>=混合赋值运算符

二、运算符的种类

1)算数运算符

举例1

package operator;     // 新创建的包

public class Demo01 {
    public static void main(String[] args) {
        // 二元运算符
        // ctrl +D
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);  // 0   0.5取0  加上强转 0.5

    }
}

举例2

package operator;

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

        //有 long 的类型,相加以后变为long
        //有 double 的类型,相加以后变为long
        //也就是说会变成最高类型的
        System.out.println(a+b+c+d);  //1231232321213364  Long
        System.out.println(b+c+d);  //141  Int
        System.out.println(c+d);   //18   Int
        System.out.println((double) c+d);   //18   Int

    }
}

举例3

package operator;

public class Demo04 {
    public static void main(String[] args) {
        //++   --   自增 , 自减
        //一元运算符
        int a = 3;
        int b = a++;  // b=3  a=4  先赋值,后自增
        int c = ++a;  // a=5  c=5  先自增,后赋值

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

        //幂运算 2^3  2*2*2 = 8
        //变 Math.pow(2,3)  + alt + enter
        double pow = Math.pow(2, 3);
        System.out.println(pow);  // 9.0

    }
}

2)关系运算符

package operator;

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

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

        System.out.println(c%a); //1   21/10=2````1

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

    }
}

3)逻辑运算符

package operator;

//逻辑运算符
public class Demo05 {
    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 c = 5;
        boolean d = (c<4)&&(c++<4);  //这里等于错,不执行c++
        System.out.println(d);  //false
        System.out.println(c);  // 5
    }
}

4)位运算符

package operator;

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
        最快 位运算  效率极其高
        << 左移
        >> 右移
        
         */

        System.out.println(2<<3);
    }
}

5)条件运算符

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?"不及格":"及格"; // 必须掌握
        // if
        System.out.println(type);   //不及格
        


    }
}

6)扩展运算符

package operator;

//扩展运算符
public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

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

        System.out.println(a);

        //字符串连接符   + , String 字符串类型
        //+ 两边只要有一边有 字符串(String)类型,就会把他们连起来
        System.out.println(a+b);  //30
        System.out.println(""+a+b); //1020
        System.out.println(a+b+""); // 30  先加,然后运算

    }

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

-Blue.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值