java基础04 运算符基本操作

学习目标:

理解运算符操作


学习内容:

1、算术运算符
2、关系运算符
3、逻辑运算符
4、位运算符
5、扩展赋值运算符
6、三元运算符


学习产出:

1、算术运算符

算术运算符:+、-、*、/、%、++、–
注意:++ 和 – 的使用

public class hello {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        System.out.println(a+b); //加
        System.out.println(a*b); //乘
        System.out.println(a-b); //减
        System.out.println(a/b); //除
        System.out.println(a%b); //取余
		//注意使用++ -- 
		int c = a++; //先赋值,后+1
        System.out.println(c); // 先将a赋值给c,后a进行+1,所以这里a还是10
        System.out.println(a); // 上面后+1了,这时候a是11
        int d = ++a; //先+1 后赋值 
        System.out.println(d);
    }
}

2、关系运算符

关系运算符:>、<、>=、<=、==、!=

public class hello {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        System.out.println(a>b); // true
        System.out.println(a<b); // false
        System.out.println(a==b);// false
        System.out.println(a!=b);// true 
    }
}

3、逻辑运算符

逻辑运算符:与 &&、或 ||、非 !

public class hello {
    public static void main(String[] args) {
       boolean a = true;
       boolean b = false;
        System.out.println(a||b); // true
        System.out.println(a&&b); // false
        System.out.println(!(a||b)); // false
    }
}

4、位运算符(了解)

  • 位与(&):二元运算符,两个为1时结果为1,否则为0
  • 位或(|):二元运算符,两个其中有一个为1时结果就为1,否则为0
  • 位异或(^):二元运算符,两个数同时为1或0时结果为1,否则为0
  • 位取非(~):一元运算符,取反操作
  • 左移(<<):一元运算符,按位左移一定的位置。高位溢出,低位补符号位,符号位不变。
  • 右移(>>):一元运算符,按位右移一定的位置。高位补符号位,符号位不变,低位溢出。
  • 无符号右移(>>>):一元运算符,符号位(即最高位)保留,其它位置向右移动,高位补零,低位溢出。

5、赋值运算符

赋值运算符:+=、-=、*=、、/=

public static void main(String[] args) {
       int a = 10;
       int b = 5;
       a+= b; // 可以理解成 a = a+b
       System.out.println(a); // a=15
    }

6、三元运算符

//规则: x?y:z x符合条件返回y,不符合返回z
    public static void main(String[] args) {
       int socre = 30;
       String a = socre <60? "及格":"不及格";
       System.out.println(a); //不及格
    }

总结


1、运算符是在代码中常见的操作
2、三元运算符是在控制语句中常使用
3、位运算符是可以加快运算速度,提高效率
4、注意 a++,++a的用法,位置不同赋值不同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值