01、运算符
运算符是一种特殊的符号,用以表示数据的运算、赋值和比较等。
- 算术运算符
- 赋值运算符
- 比较运算符(关系运算符)
- 逻辑运算符
- 位运算符
- 三元运算符
1.1、算术运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
class Day3Test{ public static void main(String[] args) {
int num1 = 12; int num2 = 5; int resule1 = num1 / num2; System.out.println(resule1);
int result2 = num1 / num2 * num2; System.out.println(result2);
double result3 = num1 / num2; System.out.println(result3);
double result4 = num1 / num2 + 0.0; double result5 = num1 / (num2 + 0.0); double result6 = (double)num1 / num2; double result7 = (double)(num1 / num2); System.out.println(result5); System.out.println(result6);
int m1 = 12; int n1 = 5; System.out.println("m1 % n1 = " + m1 % n1);
int m2 = -12; int n2 = 5; System.out.println("m2 % n2 = " + m2 % n2);
int m3 = 12; int n3 = -5; System.out.println("m3 % n3 = " + m3 % n3);
int m4 = -12; int n4 = -5; System.out.println("m4 % n4 = " + m4 % n4);
int a1 = 10; int b1 = ++a1; System.out.println("a1 = " + a1 + ",b1 = " + b1);
int a2 = 10; int b2 = a2++; System.out.println("a2 = " + a2 + ",b2 = " + b2);
int a3 = 10; a3++; int b3 = a3;
short s1 = 10;
s1++; System.out.println(s1);
byte bb1 = 127; bb1++; System.out.println("bb1 = " + bb1);
int a4 = 10; int b4 = a4--; System.out.println("a4 = " + a4 + ",b4 = " + b4); } }
|
算术运算符的注意问题
- 如果对负数取模,可以把模数负号忽略不记,如:5%-2=1。但被模数是负数则不可忽略。此外,取模运算的结果不一定总是整数。
- 对于除号“/”,它的整数除和小数除是有区别的:整数之间做除法时,只保留整数部分而舍弃小数部分。例如:intx=3510;x=x/1000*1000; x的结果是?
- “+”除字符串相加功能外,还能把非字符串转换成字符串.例如:System.out.println(“5+5=”+5+5); //打印结果是?5+5=55 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class AriExer{ public static void main(String[] args){ int num = 187; System.out.println("百位数:" + num/100); System.out.println("十位数:" + num%100/10); System.out.println("个位数:" + num%10); } }
|
1.2、赋值运算符
- 符号:
=
- 当“=”两侧数据类型不一致时,可以使用自动类型转换或使用强制类型转换原则进行处理。
- 支持连续赋值。
- 扩展赋值运算符:
+=, -=, *=, /=, %=
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
class SetValueTest{ public static void main(String[] args) { int i1 = 10; int j1 = 10;
int i2,j2; i2 = j2 = 10;
int i3 = 10,j3 = 20;
int num1 = 10; num1 += 2; System.out.println(num1); int num2 = 12; num2 %= 5; System.out.println(num2);
short s1 = 10; s1 += 2; System.out.println(s1);
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class MkFan{ public static void main(String[] args) { int i = 1; i *= 0.1; System.out.println(i); i++; System.out.println(i);
int m = 2; int n1 = 3; n1 *= m++; System.out.println("m=" + m); System.out.println("n1=" + n1);
int n = 10; n += (n++) + (++n); System.out.println(n); } }
|
1.3、比较运算符
- 比较运算符的结果都是
boolean
型,也就是要么是true,要么是false。 - 比较运算符“
==
”不能误写成“=” 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
class CompareTest{ public static void main(String[] args){ int i = 10; int j = 20; System.out.println(i==j); System.out.println(i = j);
boolean b1 = true; boolean b2 = false; System.out.println(b2 == b1); System.out.println(b2 = b1); } }
|
1.4、逻辑运算符
&
—逻辑与|
—逻辑或!
—逻辑非&&
—短路与||
—短路或^
—逻辑异或
- 逻辑运算符用于连接布尔型表达式,在Java中不可以写成3<x<6,应该写成x>3 & x<6 。
- “
&
”和“&&
”的区别:- 单
&
时,左边无论真假,右边都进行运算; - 双
&
时,如果左边为真,右边参与运算,如果左边为假,那么右边不参与运算。
- “
|
”和“||
”的区别同理,||
表示:当左边为真,右边不参与运算。 - 异或(
^
)与或( |
)的不同之处是:当左右都为true时,结果为false。理解:异或,追求的是“异”!
练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
class LogicTest{ public static void main(String[] args){ boolean b1 = false; int num1 = 10; if(b1 & (num1++ > 0)){ System.out.println("我现在在南京"); }else{ System.out.println("我现在在北京"); } System.out.println("num1 = " + num1);
boolean b2 = false; int num2 = 10; if(b2 && (num2++ > 0)){ System.out.println("我现在在南京"); }else{ System.out.println("我现在在北京"); } System.out.println("num2 = " + num2);
boolean b3 = true; int num3 = 10; if(b3 | (num3++ > 0)){ System.out.println("我现在在南京"); }else{ System.out.println("我现在在北京"); } System.out.println("num3 = " + num3);
boolean b4 = true; int num4 = 10; if(b4 || (num4++ > 0)){ System.out.println("我现在在南京"); }else{ System.out.println("我现在在北京"); } System.out.println("num4 = " + num4); } }
|
1 2 3 4 5 6 7 8 9 10 11
| class LogoinTest{ public static void main(String[] args){ boolean x = true; boolean y = false; short z = 42; if((z++==42)&&(y=true))z++; if((x=false)||(++z==45)) z++; System.out.println("z=" + z); } }
|
1.5、位运算符
1、位运算是直接对整数的二进制进行的运算
2、注意:无<<<
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
class BitTest{ public static void main(String[] args){ int i = 21;
System.out.println("i << 2 :" + (i << 2)); System.out.println("i << 3 :" + (i << 3)); System.out.println("i << 20 :" + (i << 20)); System.out.println("i << 27 :" + (i << 27)); int m = 12; int n = 5; System.out.println("m & n :" + (m & n)); System.out.println("m & n :" + (m | n)); System.out.println("m & n :" + (m ^ n)); int num1 = 10; int num2 = 20;
num1 = num1 ^ num2; num2 = num1 ^ num2; num1 = num1 ^ num2;
System.out.println("num1 = " + num1 + ",num2 = " + num2); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class BitTest{ public static void main(String[] args){ int num1 = 10; int num2 = 20;
num1 = num1 ^ num2; num2 = num1 ^ num2; num1 = num1 ^ num2;
System.out.println("num1 = " + num1 + ",num2 = " + num2); } }
|
1.6、三元运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
class SanTest{ public static void main(String[] args) { int m = 12; int n = 5; int max = (m > n)? m : n; System.out.println(max);
double num = (m > n) ? 2 : 1.0;
String str = (m > n) ? "m大" : ((m == n)? "m和n相等" : "n大"); System.out.println(str);
int n1 = 12; int n2 = 30; int n3 = -43;
int max1 = (n1 > n2) ? n1 : n2; int max2 = (max1 > n3) ? max1 : n3; System.out.println("三个数中的最大值是:" + max2);
int max3 = (((n1 > n2)? n1 : n2) > n3) ?((n1 > n2) ? n1 : n2) : n3; System.out.println("三个数中的最大值是:" + max3);
if(m > n){ System.out.println(m); }else{ System.out.println(n); } } }
|
02、运算符的优先级
- 运算符有不同的优先级,所谓优先级就是表达式运算中的运算顺序。如右表,上一行运算符总优先于下一行。
- 只有单目运算符、三元运算符、赋值运算符是从右向左运算的。