用运算符把常量或变量连接起来的式子
表达式的类型为表达式运算结果的数据类型
注意事项:
/ :除法运算符,得到两个数据相除的商。
特点:Java中整数除以整数结果还是整数。
%:取模(取余)运算,得到两个数据相除的余数。
特点:可以用来判断两个数是否能够整除。
应用:
public class ArithmeticOperators { public static void main(String[] args) { int a = 10; int b = 20; 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); } }
运行结果如下:
在Java中,小数也可以进行取模操作
public class ArithmeticOperators { public static void main(String[] args) { System.out.println(10%3); System.out.println(12.5%2); System.out.println(12.5%2.0); } }
易错点1:除数不能为0
public class ArithmeticOperators { public static void main(String[] args) { int a = 10; int b = 0; System.out.println(a/b); } }
会报错! Java里面,把程序在编译/运行的过程当中 产⽣的不正常的⾏为 叫做异常,图片里面的就是算术异常
易错点2:在java中,负数也是可以参与取模运算的
public class ArithmeticOperators { public static void main(String[] args) { System.out.println(10%3);//1 System.out.println(10%-3);//1 System.out.println(-10%3);//-1 System.out.println(-10%-3);//-1 } }
具体计算步骤(小学计算也很容易出错熬):
该种类型运算符操作完成后,会将操纵的结果赋值给左操作数。
public class IncOpr { public static void main(String[] args) { int a = 1; a += 2; System.out.println(a); a -= 1; System.out.println(a); a *= 3; System.out.println(a); a /= 3; System.out.println(a); a %= 3; System.out.println(a); } }
增量运算符会自动帮我们进行数据转换
public class IncOpr { public static void main(String[] args) { int a = 99; short s = 0; //s = s + a;//err:报错,short+int->int+int=>int,用short装不下,需要强转 //正确写法1: //s = (short)(s + a); //正确写法2: s += a;//+=会自动帮我们进行类型转换 System.out.println(a);//99 } }
++:自增1
– :自减1
举个栗子:
public class IncrementdecrementOpr { public static void main(String[] args) { int a = 10; int b = ++a;//a=a+1;b=a;->b=11 a=11 int c = a++;//b=a;a=a+1;->b=10 a=11; System.out.println(b); } }
常见关系运算符
关系运算符的运算结果都是布尔(boolean)类型,要么是true,要么是false
public class RelationalOpr { public static void main(String[] args) { int a = 10; int b = 20; System.out.println(a==b); System.out.println(a!=b); System.out.println(a>=b); System.out.println(a<=b); } }
结果如下:
注意:当需要多次判断时,不能连着写,比如:3<a<5
,程序与数学中是有区别的
定义:用于判断“并且”、“或者”、“除非”等逻辑关系。逻辑运算符两端一般连接值为布尔类型的关系表达式
也称短路与
布尔表达式1 && 布尔表达式2
:两个表达式都为真的时候,整个表达式才是真
举个栗子:
public class LogOperators { public static void main(String[] args) { int a = 1; int b = 2; System.out.println(a == 1 && b == 2); System.out.println(a == 1 && b > 100); System.out.println(a > 100 && b == 2); System.out.println(a > 100 && b > 100); } }
布尔表达式1 || 布尔表达式2
:只要有⼀个是真,整个表达式就是真
举个栗子:
public class LogOperators { public static void main(String[] args) { int a = 1; int b = 2; System.out.println(a == 1 || b == 2); System.out.println(a == 1 || b > 100); System.out.println(a > 100 || b == 2); System.out.println(a > 100 || b > 100); } }
语法规则:!表达式
真变假,假变真。
举个栗子:
public class LogOperators { public static void main(String[] args) { int a = 1; System.out.println(!(a==1)); System.out.println(!(a!=1)); } }
&&和||遵循短路求值的规则
System.out.println(10 > 20 && 10 / 0 == 0); // 打印 false System.out.println(10 < 20 || 10 / 0 == 0); // 打印 true
我们都知道,计算10 / 0
会导致程序抛出异常.但是上面的代码却能正常运行,说明10 / 0
并没有真正被求值.
&
和|
如果表达式结果为boolean时,也表示逻辑运算.但与&&
||
相比,它们不支持短路求值.
System.out.println(10 > 20 & 10 / 0 == 0); // 程序抛出异常 System.out.println(10 < 20 | 10 / 0 == 0); // 程序抛出异常
如果位操作符,左右两边不是布尔表达式,那么他就代表位运算,且位运算操作的是二进制位
对应位上有0就是0,对应位都是1的时候,这⼀位运算下来的结果就是1
对应位上有1,就是1
不⼀样的位上进⾏或,⼀样的位上是0
推论:哪⼀位上是1,那么这⼀位置上就不同
0变1,1变0
int a = 0x0000000f; System.out.printf("%x\n", ~a);//fffffff0 ⼗六进制:1 2 3 4 5 6 7 8 9 A B C D E F 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1111 0000 ~ f f f f f f f 0
移动的是二进制位
最左侧位不要了,最右侧补0
用法举例:
public class shftOperation { public static void main(String[] args) { int a = 0x10; System.out.printf("%x\n",a << 1); } }
最右侧位不要了,最左侧补符号位(正数补0,负数补1)
用法举例:
public class shftOperation { public static void main(String[] args) { int a = 0x10; System.out.printf("%x\n",a >> 1); } }
注意:没有⽆符号数左移,因为第⼀位是符号位,移的话把符号位移⾛了
最右侧位不要了,最左侧补0
用法举例:
public class shftOperation { public static void main(String[] args) { int a = 0xffffffff; System.out.printf("%x\n",a >>> 1);//7fffffff } }
语法格式:表达式1?表达式2:表达式3
如果表达式1为真,执⾏表达式2,否则执⾏表达式3
要么返回表达式2的结果,要么返回表达式3的结果,所以需要变量来接收
注意:表达式1必须是一个布尔表达式
用法举例:
public class CondOpr { public static void main(String[] args) { int a = 10; int b = 20; int c = a>b?a:b; System.out.println(c);//20 } }
就一句话:不确定的话,就加括号!
在Java中,对于运算符的优先级考察不多,不像C语言那样。
到此,对于Java中运算符的讲解已经完毕。
运算符之间是有优先级的. 具体的规则我们不必记忆. 在可能存在歧义的代码中加上括号即可
public class Swap { public static void main(String[] args) { System.out.println(3*4+1); System.out.println(3*(4+1)); } }