阿里云【名师课堂】Java零基础入门15 ~ 19:Java运算符

15、基础数学运算符

在这里插入图片描述

  • 不要写过于麻烦的运算,尽量写简单代码
  • 不用死记硬背(运算符、优先级)

自增与自减

如果觉得自增与自减迷惑性强的话可以灵活使用x += 1x -= 1来替换。

  • 范例:观察自增
public class TestDemo{    // 定义一个程序类
	public static void main(String args[]){
		int x = 10 ;
		int y = 20 ;
		// 先执行x += 1再进行乘法运算
		int result = ++x * y ;
		System.out.println("result =" + result) ;  // 220
		System.out.println("x =" + x) ;  // 11
		/* 先进行乘法运算再执行x += 1
		int result = x++ * y ;
		System.out.println("result =" + result) ;  // 200
		System.out.println("x =" + x) ;  // 11
		*/
	}
}

16、三目运算符(重点)

三目是一种赋值运算,根据条件判断来进行赋值。
基本使用语法:
type variable = 条件判断式 ? true赋值 : false赋值 ;

  • 范例:将两个变量中较大的赋值给新变量
public class TestDemo{    // 定义一个程序类
	public static void main(String args[]){
		int x = 10 ;
		int y = 20 ;
		// type variable = 条件判断式 ? true赋值 : false赋值 ;
		// 如果x > y判断结果为true,x值赋给result;结果为false,y值赋给result
		int result = x > y ? x : y ;
		System.out.println(result) ;
	}
}

如果觉得三目表达式迷惑性强的话可以用if语句来判断、赋值。当然最好还是用三目运算符,因为其更简洁。

17、关系运算符

  • >、<、>=、<=、!=、==

所有关系运算符返回结果都是boolean型。

  • 关系运算符也可以进行字符与数字的比较,这时实际是将字符的编码与数字相比较。
  • 范例:观察关系运算符
public class TestDemo{    // 定义一个程序类
	public static void main(String args[]){
		int x = 10 ;
		int y = 20 ;
		System.out.println(x > y) ;
		System.out.println(x < y) ;
		System.out.println(x >= y) ;
		System.out.println(x <= y) ;
		System.out.println(x != y) ;
		System.out.println(x == y) ;
		System.out.println('a' == 97) ;
		System.out.println('0' == 0) ;
	}
}

在这里插入图片描述

18、逻辑运算符(重点)

  • 与、或、非

麻烦之处在于,因为它们各有两种写法。

  • 与:&&&
  • 或:|||

与操作

在多个条件进行与运算时,只有所有的条件都为true才可以判定为true。理解:有0就0,全1才1。

  • 范例:观察单与操作
public class TestDemo {    // 定义一个程序类
	public static void main(String args[]) {
		// 设置一个有错误的条件看看会不会参与到运算中
		if (1 > 2 & 10 / 0 == 0) {
			System.out.println("条件满足!") ;
		}
		// 报错:除数为0
	}
}
  • 范例:观察双与操作
public class TestDemo {    // 定义一个程序类
	public static void main(String args[]) {
		// 设置一个有错误的条件看看会不会参与到运算中
		if (1 > 2 && 10 / 0 == 0) {
			System.out.println("条件满足!") ;
		}
	}
}

在这里插入图片描述

  • 通过上面两个程序比较我们可以看出:单与&会判断所有条件,不管true或false,因此判断到第二条有错误的条件时会报错;双与&&判断到所有条件中第一条为false的判断之后会停止运算,因为后面条件无论如何判断结果最终都会是false。因此双与&&又称短路与,双与&&的性能更好。

或操作

在多个条件进行或运算时,只要所有的条件中有一个是true就可以判定为true。理解:有1就1,全0才0。

  • 范例:观察单或操作
public class TestDemo {    // 定义一个程序类
	public static void main(String args[]) {
		if (1 != 2 | 10 / 0 == 0) {
			System.out.println("条件满足!") ;
		}
		// 报错:除数为0
	}
}
  • 范例:观察双或操作
public class TestDemo {    // 定义一个程序类
	public static void main(String args[]) {
		if (1 != 2 || 10 / 0 == 0) {
			System.out.println("条件满足!") ;
		}
	}
}

在这里插入图片描述

  • 通过上面两个程序比较我们可以看出:单或|会判断所有条件,不管true或false,因此判断到第二条有错误的条件时会报错;双或||判断到所有条件中第一条为true的判断之后会停止运算,因为后面条件无论如何判断结果最终都会是true。因此双或||又称短路或,双或||的性能更好。

所以以后都使用短路与&&、短路或||

非操作

&&||是二目运算符,!是单目运算符

19、位运算符(了解)

位运算符可以用于逻辑运算,逻辑运算符不能用于位运算,在18已经讨论过这个问题。
位运算是按照二进制、八进制、十六进制进行数据处理,最常用的还是二进制。在位运算中可以采用数据移位的方式实现一些数据的内容变化。

  • 位运算的实现关键问题在于十进制与二进制的转换:数据除以2取余,直到商为0,把所有的余数倒序排列(注意Java里int型是32位)。
  • 八进制标志:0+数字;十六进制标志:0x+数字

位运算:按位与&、按位或|、按位非~、按位异或^ 、左移<<、右移>>、不带符号右移>>>

  • 按位与&:双目运算符,有0就0,全1才1;
  • 按位或|:双目运算符,有1就1,全0才0;
  • 按位非~:单目运算符,0非是1,1非是0;
  • 按位异或^ :双目运算符,相同是0,不同是1。
    • 范例:观察位运算
public class TestDemo {    // 定义一个程序类
	public static void main(String args[]) {
		int numA = 14 ;  // 00000000 00000000 00000000 00001110
		int numB = 7 ;  // 00000000 00000000 00000000 00000111
		System.out.println(numA & numB) ;  // 6  00000110
		System.out.println(numA | numB) ;  // 15  00001111
		System.out.println(~numA) ;  // -15  10000000 00000000 00000000 00001111
		System.out.println(numA ^ numB) ;  // 9  00001001
	}
}
  • 左移<<:x << y —— x向左移动y位,低位补零
  • 右移>>:x >> y —— x向右移动y位,正数高位补零,负数高位补1;
  • 不带符号右移>>>:x >>> y —— x向右移动y位,高位补0
    • 范例:观察移位操作
public class TestDemo {    // 定义一个程序类
	public static void main(String args[]) {
		int numA = 7 ;  // 00000000 00000000 00000000 00000111
		System.out.println(numA << 4) ;  // 112  01110000
		System.out.println(numA >> 4) ;  // 0  00000000
		System.out.println(numA >>> 4) ;  // 0  00000000
	}
}

总结:&&&|||的区别

  • &&表示逻辑与运算,若干个判断条件中如果前面有条件返回了false,那么后面的条件不再判断,最终结果就是false;
  • &表示按位与运算和逻辑与运算。当表示按位与运算时如果有一个0结果就是0,全为1时结果才是1;当表示逻辑与运算时要比较所有的判断条件;
  • ||表示逻辑或运算,若干个判断条件中如果前面有条件返回了true,那么后面的条件不再判断,最终结果就是true;
  • |表示按位或运算和逻辑或运算。当表示按位或运算时如果有一个1结果就是1,全为0时结果才是0;当表示逻辑或运算时要比较所有的判断条件;
posted @ 2020-06-05 23:14  溺水的情书  阅读(142)  评论(0编辑  收藏  举报