Java笔记(三)运算符的使用

运算符

在这里插入图片描述

Java中的语句有很多形式,表达式就是其中一种,表达式由操作数与运算符所组成。
操作数可以是常量,变量,运算符就是就是在执行的运算表达式中的符号,具体可分为以下6类:

  1. 算数运算符
  2. 关系运算符
  3. 逻辑运算符
  4. 位运算符
  5. 三元运算符
  6. 算数优先级

算数运算符

算术运算符是对数值类型的变量进行运算的,它们的作用和在数学中的作用一样

运算符描述例子结果
+加法/正号2 + 810
-减法/负号2 - 8-6
*乘法2 * 816
/除法8 / 24
%取余(取模)8 % 20
++自增(前):先运算后赋值a = 1;b=++a;a=2;b=2;
++(后)自增:先赋值后运算a = 1;b=a++;a=2;b=1;
自减(前):先运算后赋值a = 1;b=–a;a=0;b=0;
(后)自减:先赋值后运算a = 1;b=a–;a=0;b=1;

public class ArithmeticOperation {
    public static void main(String[] args) {
        System.out.println(5 / 2); //输出为2
        //不可以两个int变量直接做除法,会丢失精度
        System.out.println(5.0 / 2); //输出为2.5
        // 将其中任意一个操作数转换为浮点数,结果为double型,Java的浮点型常量默认为double型
        System.out.println(5%2);//输出为1,余数为1
        System.out.println(2%5);//输出为2,当左操作数小于右操作数结果为左操作数
        System.out.println(5%5);//输出为0,两操作数相等时结果为零
        System.out.println(-2%5);//输出为-2,有负数看左操作数
        System.out.println(2%-5);//输出为 2,
        
        int i = 5;
        int j = 0;
        j = i++;
        System.out.println("i=" + i + "j=" + j);//输出为i=6 j=5
        //j = i++; 等价 j = i;  i = i + 1;
        j = ++i;
        System.out.println("i=" + i + "j=" + j);//输出为i=7 j=7
        // j = ++i; 等价 i = i + 1;  j = i;
    }
}

使用注意:
设a,b两个变量,取余的运算规则则是
*a%b=a- a/b b

关系运算符

关系运算符的结果都是 boolean 型,要么true要么false
关系表达式 经常用在 if 结构的条件中或循环结构的条件中
在这里插入图片描述

public class RelationalOperation {
 
  public static void main(String[] args) {
     int a = 10;
     int b = 20;
     System.out.println(a == b );//输出为false
     System.out.println(a != b );//输出为true
     System.out.println(a > b  );//输出为false
     System.out.println(a < b  );//输出为true
     System.out.println(b >= a );//输出为true
     System.out.println(b <= a );//输出为flase
  }
}

位运算符

在这里插入图片描述

public class BitOperation {
  public static void main(String[] args) {
     int a = 60; /* 60 = 0011 1100 */ 
     int b = 13; /* 13 = 0000 1101 */
     int c = 0;
     c = a & b;       /* 12 = 0000 1100 */
     System.out.println("a & b = " + c );
 
     c = a | b;       /* 61 = 0011 1101 */
     System.out.println("a | b = " + c );
 
     c = a ^ b;       /* 49 = 0011 0001 */
     System.out.println("a ^ b = " + c );
 
     c = ~a;          /*-61 = 1100 0011 */
     System.out.println("~a = " + c );
 
     c = a << 2;     /* 240 = 1111 0000 */
     System.out.println("a << 2 = " + c );
 
     c = a >> 2;     /* 15 = 1111 */
     System.out.println("a >> 2  = " + c );
  
     c = a >>> 2;     /* 15 = 0000 1111 */
     System.out.println("a >>> 2 = " + c );
  }
} 

在这里插入图片描述

逻辑运算符

请添加图片描述

public class LogicOperation{
  public static void main(String[] args) {
    
     System.out.println(1 > 0 && 2 > 0);//输出为true
     //两个都为真时,返回true
     System.out.println(1 > 0 || 2 > 5);//输出为true
     //有一个为真返回true
     System.out.println( !(1 > 0));//输出为false
     //true的相反false,返回false

  }
}
public class LogicOperation{
  public static void main(String[] args) {
    
     System.out.println(1 < 0 && 2 > 0);//输出false
     //1<0 false “&&”造成短路2>0 不做运算
     System.out.println(1 > 0 || 2 > 5);//输出为true
	//1>0 true "||"造成短路 2>5 不做运算

  }
}

使用细节:

  1. ‘’&&","||"属于短路逻辑运算符。对于逻辑运算符“&&”,要求左右两个表达式都为true时才返回true,如果左边第一个表达式为false时,它立刻就返回false,就像短路了一样立刻返回。
  2. 对于逻辑运算符“||”,要求左右两个表达式有一个为true时就返回true,如果左边第一个表达式为true时,它立刻就返回true。
  3. 逻辑与运算符:& 在用于逻辑与时,和 && 的区别是不具有短路性。所在通常使用逻辑与运算符都会使用 &&,而 & 更多的适用于位运算。

三元运算符

操作语法:
条件表达式 ? 表达式 1: 表达式2;
三元运算符也称三目运算符有三个操作数。操作流程如下:
首先判断条件,如果条件满足,就会赋予一个变量表达式1(冒号:之前的),不满足会赋予变量另外的一个表达式(冒号之后的)。

public class TernaryOperation{
  public static void main(String[] args) {
   		int x = 10;
   		int y = 20;
   		int c = x > y ? x : y;
   		//如果x的值大于y的值,就将x的值赋给c,否则将y的值赋给c;
   		//10<20 为false,所以将表达式2的值赋给c
   		 System.out.println(c);//20
  }
}

三元运算符本质上就是if—else语句的简写形式,以上的操作完全可以转换成if—else语句;

public class TernaryOperation{
  public static void main(String[] args) {
  		int x = 10;
   		int y = 20;
   		int c;
   	if(x>y)
		c=x;
	else
		c=y;
	System.out.println(c);
  }
}

算数优先级

运算符有不同的优先级,所谓优先级就是表达式运算中的运算顺序,数字越小表示优先级越高。
结合性:当使用同一优先级的运算符时,结合性决定谁先被处理。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

coding技匠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值