-
算术运算符
1.1运算符和表达式
运算符: 常量或者变量进行操作的符号
表达式: 用运算符把常量或者变量连接起来符合java语法的式子称作表达式。
不同运算符连接法人表达式体现的是不同类型的表达式。举例说明:
int a = 10;
int b = 20;
int c = a + b;“ + ” :运算符,并且是算数运算符
“a + b” : 表达式,由于+是算数运算符,所以这个表达式是算数表达式public class OperatorDemo01{ public static void main(String[] args){ //定义两个变量 int a = 1; int b = 2; 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); //除法得到的是商,取余得到的是余数 //整数相除只能得到整数,必须有浮点数的参与 System.out.println(6.0 / 4); } }
-
note:/ 和 % 的区别:两个数据做除法,/ 取结果的商,% 取结果的余数
-
整数操作只能得到整数,要想得到小数,必须要于浮点数参与运算
1.2字符的“+”操作
算数表达式中包含多个基本数据类型的值得时候,整个算数表达式的类型会自动提升
提升规则: -
byte类型,short类型和char类型将被提升为int类型
-
整个表达式的类型自动提升到表达式中最高等级操作数同样的类型
-
登记顺序:byte,short,char,int,long,float,double
/*
字符的"+"操作
*/
public class OperatorDemo02{
public static void main(String[] args){
//定义两个变量
int i = 10;
char c = 'A';//'A'的值为65;
c = 'a';//'a'的值是97;
c = '0';//'0'的值是48;
System.out.println(i + c);
//char ch = i + e;
//char类型会自动提升为int类型
int j = i + c;
System.out.println(j);
//int k = 10 + 13.14;
double d = 10 + 13.14;
System.out.println(d);
}
}
1.3字符串的“+”操作
当 “+” 操作中出现字符串时,这个 “+” 是字符串连接符,而不是算术运算
- “peter” + 1020
在 “+” 操作中,如果出现字符串,就是连接运算符,否则就是算数运算。当连续进行 “+” 操作时,从左到右逐个执行。
- 1 + 99 + “peter”
/*
字符串的"+"操作
*/
public class OperatorDemo03{
public static void main(String[] args){
System.out.println("peter" + "zhang");
System.out.println("peter" + 1020);
System.out.println(1020 + "peter");
System.out.println("peter" + 10 + 20);
System.out.println(10 + 20 + "peter");
}
}
2. 赋值运算符
/*
赋值运算符
*/
public class OperatorDemo{
public static void main(String[] args){
//把10赋值给int类型的变量;
int i = 10;
System.out.println("i:" + i);
// += 把左边和右边的数据做加法操作,结果赋值给右边
i += 20;
//i = i + 20;
System.out.println("i:" + i);
//注意,扩展的赋值运算符底层蕴含了强制转换类型
short s = 10;
s += 20;
// s = (short)(s + 20);
System.out.println("s:" + s);
}
}
- note:扩展的赋值运算符隐含了强制类型转换
-
自增自减运算符
3.1自增自减运算符
/* 自增自减运算符 */ public class OperatorDemo{ public static void main(String[] args){ //定义变量 int i = 10; System.out.println("i:" + i); //单独使用 //i++; //++i; //System.out.println("i:" + i); //参与操作使用 //int j = i++; int j = ++i; System.out.println("i:" + i); System.out.println("j:" + j); } }
notes:- ++和–既可以放在变量后边,也可以放在变量的前边。
- 单独使用的时候,++和–无论是放在变量的前边还是变量的后边,结果是一样的。
- 参与操作的时候,如果放在变量的后边,先拿变量参与操作,后拿变量做++或–;
- 参与操作的时候,如果放在变量的前边,先拿变量做++或者–,后拿变量参与操作。
-
关系运算符
4.1关系运算符/* 关系运算符 */ public class OperatorDemo{ public static void main(String[] args){ //定义变量 int i = 10; int j = 20; int k = 10; //== System.out.println(i == j); System.out.println(i == k); //!= System.out.println(i != j); System.out.println(i != k); //> System.out.println(i > j); System.out.println(i > k); //>= System.out.println(i >= j); System.out.println(i >= k); //不下心把==写成了= //把j的值赋值给了i,然后输出i的值 System.out.println(i = j); } }
notes:- 关系运算符的结果都是boolean类型,要么是true,要么是false;
- 千万不要把“==”写成“=”。
-
逻辑运算符
5.1逻辑运算符概述
在数学中,一个数据x,大于3,小于6,我们可以这样来进行表示:3<x<6。
在Java中,需要把上面的式子先进行拆解,再进行合并表达- 拆解为:x>3和x<6
- 合并为:x>3&&x<6
&&其实就是一个逻辑运算符;
逻辑运算符,是用来连接关系表达式的运算符
逻辑运算符也可以直接连接布尔类型的常量或者变量
/* 逻辑运算符 */ public class OperatorDemo{ public static void main(String[] args){ //定义变量 int i = 10; int j = 20; int k = 30; //& 有false则false System.out.println((i > j) & (i > k)); System.out.println((i < j) & (i > k)); System.out.println((i > j) & (i < k)); System.out.println((i < j) & (i < k)); System.out.println("--------"); //| 有true则true System.out.println((i > j) | (i > k)); System.out.println((i < j) | (i > k)); System.out.println((i > j) | (i < k)); System.out.println((i < j) | (i < k)); System.out.println("--------"); //^ 相同为false,不同为true System.out.println((i > j) & (i > k)); System.out.println((i < j) & (i > k)); System.out.println((i > j) & (i < k)); System.out.println((i < j) & (i < k)); System.out.println("--------"); //! System.out.println((i > j)); System.out.println(!(i > j)); System.out.println(!!(i > j)); System.out.println(!!!(i > j)); } }
5.2逻辑运算符
5.3短路逻辑运算符
/* 短路逻辑运算符 */ public class OperatorDemo{ public static void main(String[] args){ //定义变量 int i = 10; int j = 20; int k = 30; //& 有false则false System.out.println((i > j) && (i > k)); System.out.println((i < j) && (i > k)); System.out.println((i > j) && (i < k)); System.out.println((i < j) && (i < k)); System.out.println("--------"); //| 有true则true System.out.println((i > j) ||(i > k)); System.out.println((i < j) ||(i > k)); System.out.println((i > j) ||(i < k)); System.out.println((i < j) ||(i < k)); System.out.println("--------"); //&&和& //System.out.println((i++ > 100) & (j++ > 100)); System.out.println((i++ > 100) && (j++ > 100)); System.out.println("i:" + i); System.out.println("j:" + j); } }
notes:- 逻辑与&,无论左边真假,右边都要执行
- 短路与&&,如果左边为真,右边执行;如果右边为假,右边不执行
- 逻辑或|,无论左边真假,右边都要执行
- 短路或||。如果左边为假,右边执行;如果左边为真,右边不执行
-
三元运算符
格式:关系表达式?表达式1:表达式2;
范例:a > b ? a : b;
计算规则
首先计算关系表达式的值
如果值为true,表达式1的值就是运算结果
如果值为false,表达式2的值就是运算结果/* 三元运算符 */ public class OperatorDemo{ public static void main(String[] args){ //定义变量 int a = 10; int b = 20; //获取两个数据中的较大值 int max = a > b ? a : b; //输出结果 System.out.println("max:" + max); } }
案例: 三个和尚
需求: 一座寺庙里住着三个和尚,已知他们的身高分别为150cm,210cm,165cm,请用程序实现获取这三个和尚的最高身高
分析: 1.定义三个变量用于保存和尚的身高,单位为cm
int height1 = 150;
int height2 = 210;
int height3 = 165;
2.用三元运算符获取前两个和尚的较高身高值,并用临时变量保存起来
(height1 > height2) ? height1:height2;
3.用三元运算符获取临时身高值和第三个和尚身高较高值,并用最大身高变量保存
4.输出结果/* 三个和尚 需求:一座寺庙里住着三个和尚,已知他们的身高分别为150cm,210cm,165cm, 请用程序实现获取这三个和尚的最高身高 */ public class OperatorTest{ public static void main(String[] args){ //1.定义三个变量用于保存和尚的身高,单位为cm int height1 = 150; int height2 = 210; int height3 = 165; //2.用三元运算符获取前两个和尚的较高身高值,并用临时变量保存起来 int tempHeight = (height1 > height2) ? height1:height2; //3.用三元运算符获取临时身高值和第三个和尚身高较高值,并用最大身高变量保存 int maxHeight = tempHeight > height3 ? tempHeight : height3; //4.输出结果 System.out.println("maxHeight:" + maxHeight); } }
tips: 三元运算符多加练习后即可熟练掌握