5.Java中的运算符

上一篇:4.Java的变量和常量



一.分类

概述运算符
算术运算符+,-,*,/,%,++,- -
关系运算符>,<,>=,<=,==,!=
逻辑运算符!,&&,||
位运算符>>,<<,>>>,&,|,^,~
赋值运算符=,及其扩展赋值运算符如+=,-=,*=,/=等
条件运算符?,:
对象运算符·
下标运算符[]
实例运算符instanceof
内存分配运算符new
强制类型转换运算符(类型)
方法调用运算符()

二.算术运算符

1.概括

常见的算术运算符包含以下:
算术运算符

2.++和–

举例说明:

public class Test07 {
	public static void main(String[] args) {
		int a = 5;
		int b = a++;
		System.out.println(b);//5
		System.out.println("--------------");
		int c = 4;
		int d = 5;
		int e = (c++)+(++d);
		System.out.println("c="+c);//5
		System.out.println("d="+d);//6
		System.out.println("e="+e);//10
		//c++先参与运算再自增,++d先自增再参与运算
		//同样,i--也是先参与运算再自增,++先自增再参与运算
		int f = 4;
		int g = 5;
		int h = (f--)+(--g);
		System.out.println(h);//8
		
		int x = 5;
		System.out.println(x--);//5
		System.out.println(x);//4
	}

}

3.Java中算术运算符的运算规则

1).计算的封闭性,即同一种数据类型参与运算,结算结果也是相同的数据类型。
例如:

public class Test09 {
	public static void main(String[] args) {
		int a = 5;
		int b = 2;
		System.out.println(a/b);//输出整数2
		
		double c = a/b;
		System.out.println(c);//输出2.0
		//这里需要注意的是:原本结果应该是int类型的2
		//但是将结果赋值给了double类型的变量
		//所以int类型转换为double类型,结果为2.0
	}
}

2).当byte,char,short,类型的变量参与算术运算时,JVM会首先将byte,char,short处理成32位长再进一步操作:也就是说JVM会将byte,char,short变量转换为int类型再继续运算。
例如:

public class Test10 {

	public static void main(String[] args) {
		byte a = 3;
		byte b = 4;
		
		//byte c = a+b;
		//Type mismatch: cannot convert from int to byte
		
		byte c = (byte)(a+b);
		System.out.println(c);//输出7
		
		int d = a+b;
		System.out.println(d);//输出7
	
		char ch = 'a';
		int i = 1;
		int e = ch;
		System.out.println(e);//输出97
		System.out.println(ch+i);//输出98
	}
}

3).两个不同类型的操作数参与运算时,系统会将两者先统一为较大范围者的数据类型在进行运算。
例如:

public class Test11 {
	public static void main(String[] args) {
		int a = 10;
		byte b = 56;
		
		//byte c = a+b;
		//Type mismatch: cannot convert from int to byte
		
		int d = a+b;
		System.out.println(d);//输出66
	}
}

三.赋值运算符

常见的赋值运算符包含以下:
赋值运算符需要注意的是:在使用扩展赋值运算符时会自动完成强制类型转换。

public class Test12 {
	public static void main(String[] args) {
		short x = 10;
		int y = 3;
		
		//x=x+y;
		//Type mismatch: cannot convert from int to short
		
		x += y;
		//会被强制转换为int
		System.out.println(x);
	}
}

四.关系运算符

常见的关系运算符有:
关系运算符
关系运算符对数值型、布尔型和引用类型有效,其返回值为布尔值。
例如:

public class Test02 {
	public static void main(String[] args) {
		// 关系运算符,返回值为布尔值
		int a = 10;
		int b = 5;
		boolean result = (a>b);
		System.out.println(result);//输出true
		
		boolean result1 = (a<=b);
		System.out.println(result1);//输出false
	}
}

五.逻辑运算符

常见的逻辑运算符有:
逻辑运算符例如:

public class Test03 {
	public static void main(String[] args) {
		// 逻辑运算符
		int a = 1;
		int b = 2;
		int c = 3;
		//与运算(&、&&):一假则假
		boolean result = (a<b)&(c>b);
		System.out.println(result);
		boolean result1 = false && c>b;
		                          //c>b:Dead code
		System.out.println(result1);
		
		//或运算:(|、||):一真则真
		System.out.println(true|false);
		System.out.println(true||false);
		                        //false:Dead code 
		
		//逻辑非:!
		System.out.println("!true= "+!true);
		System.out.println("!false= "+!false);
		
		//异或:^
		//两边结果一致是false,不一致结果是true
		System.out.println("true^true = "+(true^true));
		System.out.println("false^false = "+(false^false));
		System.out.println("false^true = "+(false^true));
	}
}

六.三元运算符

三元运算符的格式:布尔表达式?为true代码块:为false代码块
例如:

public class Test04 {
	public static void main(String[] args) {
		// 三元运算符
		//格式:
		//    布尔表达式?true代码块:false代码块
		//判断奇偶数
		int a = 10;
		System.out.println(a%2==0?"偶数":"奇数");
		
		//比较两个数大小
		int b = 5;
		System.out.println(a>b?a+"更大":b+"更大");
	}
}


七.其他运算符

除此之外,在Java中还有移位运算符、下标运算符、实例运算符、强制类型转换运算符。由于不常用或者在后续笔记中会列出,在此就不做更多的介绍。
下一篇:6.Java选择语句if……else和switch语句

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值