目录
一、运算符概述
实际生活和工作中,经常会分析解决各种运算问题,而Java编写的程序或项目,也是解决实际问题,同样会涉及到大量的运算。
不仅Java语言,大部分语言都包含如下运算符:
1. 赋值运算符
2. 算术运算符
3. 关系运算符
4. 逻辑运算符
5. 位运算符
6. 移位运算符
7. 条件运算符(三目运算符)
8. 复合运算符
二、赋值运算符
1. 符号
使用 = 表示
2. 说明:
功能是把右侧的结果,赋值到左侧的变量空间中。如:int a = 10;
赋值运算符优先级最低,若右侧是复杂的表达式,则计算的最终的结果,赋值给左侧变量。
3. 案例
public class Test {
public static void main(String[] args) {
//赋值运算符
int a = 10;
int b = a + 10 + 20;
int x, y, z;
x = y = z = 20; //正确
}
}
三、算术运算符
赋值运算符和算术运算符参考视频:https://live.csdn.net/v/282789https://live.csdn.net/v/282789
1. 符号
算术运算符和数学运算基本一致,因计算机的存储方式,稍有不同。
算术运算符包含如下符号:
2. 说明
运算符两侧类型必须一致,如果不一致会按照以前讲解的类型转换规则转换一致。
对于 / 除法符号,如果两个数都是整数,不管是否能被整数,结果只保留整数部分。
对于 / 除法符号,不能除0,会报错。
对于 % 符号,得到的是两个数相除之后的余数。常用于判断能否被整数或倍数问题。
对于++或--符号,位于变量之前,变量先自增或自减,再参与运算。
对于++或--符号,位于变量之后,先使用当前变量的值参与运算,变量再自增或自减。
自增或自减参考视频:https://live.csdn.net/v/282790https://live.csdn.net/v/282790
3. 案例
public class Test {
public static void main(String[] args) {
//算术运算符
// +, -, *, /, %(取模/求余)
int x = 20;
int y = 30;
int z1 = x + y;
int z2 = x - y;
int z3 = x * y;
int z4 = y / x;
int z5 = y % x;
//注意点:
//如果是 /, 只能得到商数,不管能否整除.前提条件两个数都是整数的时候
// 3 / 2 = 1
double z6 = 3.0 / 2;
System.out.println(z6);
int z7 = 3 / 2;
System.out.println(z7);
// % 就是余数
//如果a%b余数是0,表示b能被a整数
int z8 = 3 % 2;
System.out.println(z8);
//分子为正数,%结果为正数
//分子为负数,%结果为负数
int x0 = 10;
int y0 = 3;
System.out.println(x0 % y0);//=1
int x1 = 10;
int y1 = -3;
// System.out.println(x1 / y1);
System.out.println(x1 % y1);//=1
int x2 = -10;
int y2 = 3;
System.out.println(x2 % y2);// =-1
int x3 = -10;
int y3 = -3;
System.out.println(x3 % y3);//= -1
//自增或自减
// ++, --
// int q = 10;
// q = q + 1;
// q = q - 1;
int s = 1;
// s++; // s = s + 1;
// ++s; // s = s + 1;
// s--; // s = s - 1;
// --s; // s = s - 1;
/*
* ++/-- s++/s--
* 如果位于变量之后,先使用s当前的值参与运算,然后再自增或自减
* ++/-- ++s/--s
* 如果位于变量之前,s先自增或自减,之后再参与运算
*/
// System.out.println(s++);//1
// System.out.println(s);//2
// System.out.println(++s);//2
// System.out.println(s);//2
int m = 10;
int n = 5;
int result1 = m++ + --n + ++m + n--;
System.out.println(result1);//30
System.out.println(m);//12
System.out.println(n);//3
//复合运算符
// +=, -=, *=, /=, %=, >>=
int m1 = 5;
int n1 = 10;
// m1 += n1;//m1 = m1 + n1;
// m1 -= n1;//m1 = m1 - n1;
//注意: byte, short, char
byte bb1 = 10;
byte bb2 = 5;
//因为byte,short,char计算的时候会先自动转换成int,再计算
//所以,bb1->int, bb2->int bb1 + bb2结果为int
//必须使用强制类型转换才可以
bb1 = (byte)(bb1 + bb2);
bb1 += bb2;//bb1 = (byte)(bb1 + bb2);
bb1 -= bb2;
}
}
4. 练习
/*
* 将一个五位数倒序输出,如
* 12345----->54321
*/
public class Test {
public static void main(String[] args) {
int num = 12345;
int g = num%10;
int s = num/10%10;
int b = num/100%10;
int q = num/1000%10;
int w = num/10000;
System.out.println(""+g+s+b+q+w);
}
}
/*
* 摄氏度与华氏度的转换公式为:摄氏度=5/9.0*(华氏度-32)
* 将一个华氏度,输出对应的摄氏度
*/
public class Test {
public static void main(String[] args) {
double f= 120;
double s = 5/9.0*(f-32);
System.out.println("当前摄氏度为:"+s);
}
}
四、关系运算符
关系运算符,其实就是比较运算符,对于比较来说,最终结果只有成立或不成立两种。因此关系运算符结果是boolean类型的。
关系运算符参考视频:https://live.csdn.net/v/282791https://live.csdn.net/v/282791
1. 符号
关系运算符符号包括:>, >=, ==, !=, <, <=
后期还会学到 instanceof运算符
2. 说明
返回值为true(表示条件成立),false(表示条件不成立)。
注意,==是用于比较,=是赋值运算符。
3. 案例
/*
* 关系运算符(比较)
* 比较,有两种情况,要么成立要么不成立
* >, >=, <, <=, ==, !=, instanceof
* 比较运算符表达式结果要么为true,要么为false
*
* 使用场景:
* 条件判断语句中
* 特别注意的:
* = 赋值运算符
* == 关系运算符
*/
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
boolean f1 = a > b;
boolean f2 = a >= b;
boolean f3 = a < b;
boolean f4 = a <= b;
boolean f5 = a == b;
boolean f6 = a != b;
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
System.out.println(f4);
System.out.println(f5);
System.out.println(f6);
}
}
五、逻辑运算符
逻辑运算符参考视频:https://live.csdn.net/v/282792https://live.csdn.net/v/282792
关系运算符仅表示一个条件是否成立,现实判断中,常多个条件组合判断一个结果。此时就用到逻辑运算符。
逻辑运算符要求两侧的运算类型必须是boolean类型。逻辑运算符就是把多个boolean类型的值连接在一起。逻辑运算符的运算结果也是boolean类型。
1. 符号
逻辑与:&&(有短路的与) 或 &(无短路的与)
逻辑或:||(有短路的或) 或 |(无短路的或)
逻辑非:!(取反)
2. 说明
1)用于多个条件组合判断,结果为boolean,常用在if, 循环中的条件判断中
2)&&或&,要求两个条件同时为true,结果为true
3)||或|,要求两个条件有一个为true,结果为true
4)!(条件表达式),条件表达式为true,结果为false,表达式为false,结果为true。
5)有短路操作表示一个逻辑运算符表达式,前面的一个条件已经决定最终结果,则后面的条
件不再执行;无短路操作表示前面条件不管是否决定最终结果,后面条件都会执行。
3. 案例
/*
* 逻辑运算符:
* 用于多个条件组合判断的。
* 结果是多个条件的最终组合结果。true or false
* 使用场景:条件表达式语句中
*
* &&:逻辑与 有短路操作
* condition1 && condition2 &&.....
* 所有表达式同时为true,结果为true。有一个为false,结果就为false
*
* ||:逻辑或 短路操作
* condition1 || condition2 || ....
* 所有表达式为false,结果为false;有一个为true,结果就为true
*
* !:逻辑非
* !condition
* 取反的操作。条件为true,!condition结果为false;反则反之。
*
* & : 逻辑与 没有短路操作
* condition1 & condition2 & .....
* 所有表达式同时为true,结果为true。有一个为false,结果就为false
*
* | : 逻辑或 没有短路操作
* condition1 | condition2 | ....
* 所有表达式为false,结果为false;有一个为true,结果就为true
*/
public class Test {
public static void main(String[] args) {
String name = "小明";
int chn = 98;
int math = 100;
int eng = 90;
boolean result1 = (chn == 100) && (math == 100) && (eng == 100);
System.out.println(result1);//false
boolean result2 = (chn == 100) || (math == 100) || (eng == 100);
System.out.println(result2);//true
//以后不准使用&, |
boolean result3 = (chn == 100) & (math == 100) & (eng == 100);
System.out.println(result3);//false
boolean result4 = (chn == 100) | (math == 100) | (eng == 100);
System.out.println(result4);//true
//短路现象 &&
// boolean result5 = (chn == 100) && (math++ == 100) && (eng++ == 100);
// System.out.println(math);//100
// System.out.println(eng);//90
// boolean result55 = (math == 100) || (chn++ == 100) || (eng++ == 100);
// System.out.println(chn);//98
// System.out.println(eng);//90
//无短路现象
// boolean result6 = (chn == 100) & (math++ == 100) & (eng++ == 100);
// System.out.println(math);//101
// System.out.println(eng);//91
// boolean result66 = (math == 100) | (chn++ == 100) | (eng++ == 100);
// System.out.println(chn);//99
// System.out.println(eng);//91
int x = 10;
boolean flag = x > 20;//false
flag = !flag;//true
System.out.println(flag);
}
}
六、位运算符
位运算符参考视频:https://live.csdn.net/v/282793https://live.csdn.net/v/282793
二进制数位的运算。需要把数据转成二进制再进行运算。二进制运算是所有运算中速度最快的运算。
一定要注意,数值是以补码形式存储的。
1. 符号
常见位运算符号:
~:按位取反
^:按位异或
&:按位与
|:按位或
2. 说明
~:按位取反,表示数值补码,包含符号位的每一位,1变成0,0变成1。
^:两个数,对应的位,相同结果为0,不同结果为1.
&:两个数对应的位,同时为1,结果为1,有一个为0或者两个都是0,结果为0。
|:两个数对应的位,有一个为1或同时为1,结果为1,同时为0,结果为0。
3. 案例
/*
* 位运算符
* ~:按位取反,每一位,1变成0,0变成1
* &:按位与,对应的位,同时为1,结果为1,有一个为0,结果就为0
* |:按位或,对应的位,同时为0,结果为0,有一个为1,结果就为1
* ^:按位异或,对应的位,相同位0,不同位1.
*/
public class Test {
public static void main(String[] args) {
int a = -10;
/* 数值在计算机中以补码存储的。
* 正数的原码,反码,补码相同
* 负数原码,是其正数原码,符号位为1
* 负数反码,是其原码符号位不变,其他为按位取反
* 负数补码,是其反码末位+1
* a原码 = 10000000 00000000 00000000 00001010
* a反码 = 11111111 11111111 11111111 11110101
* a补码 = 11111111 11111111 11111111 11110110
*
* ~a:任意位,包含符号位,1-》0, 0-》1
* ~a = 00000000 00000000 00000000 00001001 = 9 在计算机中存储的,补码
*/
System.out.println(~a);//9
//按位与
int x1 = 10;
int y1 = 5;
/*
* x1 = 00000000 00000000 00000000 00001010
* y1 = 00000000 00000000 00000000 00000101
* & = 00000000 00000000 00000000 00000000
*/
System.out.println(x1 & y1);
//按位或
/*
* x1 = 00000000 00000000 00000000 00001010
* y1 = 00000000 00000000 00000000 00000101
* | = 00000000 00000000 00000000 00001111 = 15
*/
System.out.println(x1 | y1);
//按位异或
int x = 10;
int y = 5;
/*
* x = 00000000 00000000 00000000 00001010 = 10
* y = 00000000 00000000 00000000 00000101 = 5
* z ^= 00000000 00000000 00000000 00001111 = 15
*/
System.out.println(x ^ y);
int z = x ^ y;
System.out.println(z ^ x);//5-> x^y^x = y
System.out.println(z ^ y);//10-> x^y^y = x
//一个数异或同一个数两次结果为其本身。
}
}
/*
* 实现两个数的交换
*/
public class Test {
public static void main(String[] args) {
//1- 使用第三方变量
int x1 = 10;
int y1 = 20;
int temp = x1;
x1 = y1;
y1 = temp;
//2- 不准使用第三方变量,可能造成数据溢出
int x2 = 10;
int y2 = 20;
x2 = x2 + y2;// x2 = 10 + 20, y2 = 20;
y2 = x2 - y2;// x2 = 10 + 20, y2 = 10 + 20 - 20 = 10;
x2 = x2 - y2;// x2 = 10 + 20 - 10 = 20, y2 = 10
//3- 异或。
int x = 10;
int y = 20;
x = x ^ y; // x=10^20, y=20
y = x ^ y; // x=10^20, y=10^20^20=10
x = x ^ y; // x=10^20^10=20, y=10
}
}
七、移位运算符
移位运算符参考视频:https://live.csdn.net/v/282794https://live.csdn.net/v/282794
移位运算符也是位操作运算符的一种,只是单独说明而已。
1. 符号
<<:有符号左移
>>:有符号右移
>>>:无符号右移
2. 说明
<<:如x << n,高位去掉n位,低位以0补n位,等价于x乘以2的n次方。
>>:如x >> n,低位去掉n位,高位以符号位填充n位,等价于x除以2的n次方。
>>>:如x >>> n,低位去掉n位,高位以0补n位,相等于x除以2的n次方。
注意:
1)对于byte,short,char类型数据,会先自动转换成int再移位。
2)int类型整数移位,如果超过了32位,则系统会先用移动的数与32取模,结果是数据的移动
位数。如x >> 34,则移动位数为,34%32 = 2, 等价于x >> 2。long也是如此。
3. 案例
/*
* <<, >>, >>>
<< 有符号的左移 x << n 高位去掉n位,低位以0补n位,相当于x乘以n个2;
>> 有符号的右移 低位去掉n位,高位以符号位补n位,相当于x除以n个2;
>>> 无符号右移 低位去掉n位,高位以0补n位,相当于x除以n个2;
*/
public class Test {
public static void main(String[] args) {
int a1 = 2;
/*
* 2 = 00000000 00000000 00000000 00000010
* 2 << 2
* 000000 00000000 00000000 0000001000
*/
a1 = a1 << 2;
System.out.println(a1);
int a2 = -2;
/*
* a2 = 10000000 00000000 00000000 00000010
* 反码 = 11111111 11111111 11111111 11111101
* 补码 = 11111111 11111111 11111111 11111110
* a2 << 3
* 补码 11111 11111111 11111111 11111110000
* 反码 11111 11111111 11111111 11111101111
* 原码 10000 00000000 00000000 00000010000
*/
System.out.println(a2 << 3);
int b = 248;
/*
* 248 = 00000000 00000000 00000000 11111000
* 000000000000 00000000 00000000 1111 = 15
*/
System.out.println(b >> 4);
int b2 = 248;
/*
* 248 = 00000000 00000000 00000000 11111000
* 0000000000 00000000 00000000 111110
*/
System.out.println(b2 >>> 2);
/*
* 248 = 10000000 00000000 00000000 11111000
* 11111111 11111111 11111111 00000111
* 补码 11111111 11111111 11111111 00001000
* 补码 0011111111 11111111 11111111 000010
*/
int b3 = -248;
System.out.println(b3 >>> 2);
int x = 1073741762;
/*
* 实现将x 转变成16进制。
*/
// x & 15
// 0011111111 11111111 11111111 000010
// 0000000000 00000000 00000000 001111
}
}
八、条件运算符(三目运算符)
三目运算符参考视频:https://live.csdn.net/v/282795https://live.csdn.net/v/282795
唯一一个三目运算符。即有三个运算的表达式。
1. 符号
符合格式:条件表达式?表达式1:表达式2
2. 说明
1) 条件表达式运行的结果必须是一个boolean的结果。条件表达式的结果为true,则执行表达
式1;条件表达式的结果为false,执行表达式2。
2) 表达式1和表达式2数据类型必须相同或兼容。
3) 条件运算符可看做是if..else的简写形式。
3. 案例
/*
* 条件运算符
* 格式: condition?return_value1:return_value2
* condition是一个表达式,其值是true或false
* 条件运算符要有返回值,保证return_value1和return_value2类型一致
*/
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
int max = a > b ? a : b;
String max2 = a > b ? "a > b" : "a <= b";
System.out.println(max);
System.out.println(max2);
// a > b ? a : "a < b";//错误的,返回值类型必须一致
}
}
九、复合运算符
1. 符号
常用的符合运算符:+=, -=, *=, /=,%=,&=,|=,^=,<<=,>>=,>>>=
2. 说明
1) 以上复合运算符表达式说明
a += b 等价于 a = a + b
a -= b 等价于 a = a - b
......
2) 复合运算符具有良好性能且程序更加健壮,推荐使用
3. 案例
public class Test {
public static void main(String[] args) {
int num=10;
//num=num+10;
num+=10;//相当于num=num+10;
System.out.println("num="+num);
}
}
混合运算符好处:可以自动类型提升转换。
public class Test {
public static void main(String[] args) {
//byte,short,char都一样
byte b1 = 10;
byte b2 = 20;
// b1 = b1 + b2;//错误,b1,b2会自动转换成int,再计算
b1 = (byte)(b1 + b2);//正确,必须强制转换
b1 += b2;//正确,会自动强制转换为byte。
}
}
十、优先级和结合性
优先级不同的,优先级高的先运算;优先级相同的,看结合性。
运算符结合性和优先级不用记忆。代码编写时,最好使用(),来分割执行的先后顺序。