文章目录
1.算术运算符
1.1 运算符和表达式
-
运算符: 对常量或者变量进行操作的符号
-
表达式: 用运算符把常量或者变量连接起来符合java语法的式子就可以称为表达式
不同运算符链接的表达式体现的是不同类型的表达式
-
举例说明:
int a = 10
int b = 20
int c = a + b
“+”: 是运算符,并且是算术运算符
“a + b” : 是表达式,因为加号+是算术运算符,所以是算术表达式
1.2 算术运算符
符号 | 作用 | 说明 |
---|---|---|
+ | 加 | |
- | 减 | |
* | 乘 | |
/ | 除 | |
% | 取余 |
整数操作只能得到整数,想要得到小数,必须有浮点数参与运算
//算术运算符
public class OperatorDemo01 {
public static void main(String[] args) {
//定义两个变量
int a = 6;
int b = 4;
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.00 / b);
}
}
1.3 字符的"+"操作
拿字符char在计算机底层对应的数值来进行计算的
‘A’ | 65 | A-Z是连续的 |
---|---|---|
‘a’ | 97 | a-z是连续的 |
‘0’ | 48 | 0-9是连续的 |
算术表达式中包含多个基本数据类型的值的时候, 整个算术表达式的类型会自动进行提升
-
提升规则
-
byte类型,short类型和char类型将被提升到int类型
-
整个表达式的类型自动提升到表达式中最高等级操作数同样的类型
等级顺序: byte,short,char>int>long>float>double
-
// 字符的“+”操作
public class OperatorDemo01 {
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 + c;
//char类型会被自动提升为int类型
int j = i + c;
System.out.println(j);
//int k = 10 + 13.14;
//double类型比int类型要高
double d = 10 + 13.14;
}
}
1.4 字符串的“+”操作
当"+“操作中出现字符串时,这个”+"是字符串连接符,而不是算术运算
- “IT黑马” + 666
在"+“操作中,如果出现了字符串,就是链接运算符,否则就是算术运算.当连续进行”+"操作时,从左到右逐个执行
- 1 + 99 + “年黑马”
//字符串的"+"操作
public class OperatorDemo01 {
public static void main(String[] args) {
System.out.println("IT" + "黑马");
System.out.println("IT黑马" + 666);
System.out.println(666 + "IT黑马");
System.out.println("黑马" + 6 + 66);
System.out.println(1 + 99 + "年黑马");
}
}
2.赋值运算符
2.1 赋值运算符
符号 | 作用 | 说明 |
---|---|---|
= | 赋值 | a=10,将10赋值给变量a |
+= | 加后赋值 | a+=b,将a+b的值给a |
-= | 减后赋值 | a-=b,将a-b的值给a |
*= | 乘后赋值 | a*=b,将a乘b的值给a |
/= | 除后赋值 | a/=b,将a除以b的商给a |
%= | 取余后赋值 | a%=吧,将a除以b的余数给a |
注意事项: 扩展的赋值运算符隐含了强制类型转换
//赋值运算符
public class OperatorDemo {
public static void main(String[] args) {
//把10赋值给int类型的变量i
int i = 10;
System.out.println("i: "+ i);
// += 把左边和右边的数据做加法操作,结果赋值给左边 下面两个式子不完全等价
i += 20;
//i = i + 20;
System.out.println(i);
//注意: 扩展的赋值运算符底层隐含了强制类型转换
short s = 10;
s += 20;
//s = (short)(s + 20);
System.out.println("s: "+ s);
}
}
3. 自增自减运算符
3.1 自增自减运算符
符号 | 作用 | 说明 |
---|---|---|
++ | 自增 | 变量的值加1 |
– | 自减 | 变量的值减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);
}
}
4. 关系运算符
4.1 关系运算符
符号 | 说明 |
---|---|
== | a==b,判断a和b的值是否相等,成立为true,不成立为false |
!= | a!=b,判断a和b的值是否不相等,成立为true,不成立为false |
> | a>b,判断a是否大于b,成立为true,不成立为false |
>= | a>=b,判断a是否大于等于b,成立为true,不成立为false |
< | a<b,判断a是否小于b,成立为true,不成立为false |
<= | a<=b,判断a是否小于等于b,成立为true,不成立为false |
注意事项:关系运算符的结果都是 booolean类型,要么是true,要么是false。
//关系运算符
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);
//<
System.out.println(i < j);
System.out.println(i < k);
//<=
System.out.println(i <= j);
System.out.println(i <= k);
}
}
5. 逻辑运算符
5.1 逻辑运算符概述
在数学中,3<x<6
JAVA中,要先拆解再合并表达
- 拆解为: x>3和x<6
- 合并后: x>3&&x<6
&&其实就是一个逻辑运算符
逻辑运算符,是用来连接关系表达式的运算符
当然,逻辑运算符也可以直接连接布尔类型的常量或者变量
5.2 逻辑运算符
符号 | 作用 | 说明 |
---|---|---|
& | 逻辑与 | a&b,a和b都是true,结果为true,否则为false |
| | 逻辑或 | a|b,a和b都是false,结果为false,否则为true |
^ | 逻辑异或 | a^b,a和b结果不同为true,相同为false |
! | 逻辑非 | !a,结果和a和结果正好相反 |
//逻辑运算符
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) );//false & false
System.out.println( (i < j) & (i > k) );//true & false
System.out.println( (i > j) & (i < k) );//false & true
System.out.println( (i < j) & (i < k) );//true & true
System.out.println("------------------");
//| 或 有true则true
System.out.println( (i > j) | (i > k) );//false | false
System.out.println( (i < j) | (i > k) );//true | false
System.out.println( (i > j) | (i < k) );//false | true
System.out.println( (i < j) | (i < k) );//true | true
System.out.println("------------------");
//^ 异或 相同为false,不同为true
System.out.println( (i > j) ^ (i > k) );//false ^ false
System.out.println( (i < j) ^ (i > k) );//true ^ false
System.out.println( (i > j) ^ (i < k) );//false ^ true
System.out.println( (i < j) ^ (i < k) );//true ^ true
System.out.println("------------------");
//! 非 有false则false
System.out.println( (i > j) );//false
System.out.println( ! (i > j) );//true
}
}
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) );//false && false
System.out.println( (i < j) && (i > k) );//true && false
System.out.println( (i > j) && (i < k) );//false && true
System.out.println( (i < j) && (i < k) );//true && true
System.out.println("------------------");
//|| 短路或 有true则true
System.out.println( (i > j) || (i > k) );//false || false
System.out.println( (i < j) || (i > k) );//true || false
System.out.println( (i > j) || (i < k) );//false || true
System.out.println( (i < j) || (i < k) );//true || true
System.out.println("------------------");
//&&和&
//System.out.println((i++ > 100 ) & (j++ > 100 ));//false & false
System.out.println((i++ > 100 ) && (j++ > 100 ));//false && false 左边一旦为真 右边就不需要执行了
System.out.println("i:" + i);
System.out.println("j:" + j);
}
}
6. 三元运算符
6.1 三元运算符
- 格式:关系表达式 ? 表达式1 : 表达式2;
- 范例:a>b?a:b;
- 计算规则:
- 首先计算关系表达式
- 如果值为true,表达式1的值就是运算结果
- 如果值为false,表达式2的值就是运算结果
/*三元运算符
格式:
关系表达式 ? 表达式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);
}
}
6.2 案例:两只老虎
- 需求:动物园里有两只老虎,已知两只老虎的体重分别为180kg和200kg,请用程序实现判断两只老虎的体重是否相同。
- 分析:
- 定义两个变量用于保存老虎的体重,单位为kg,这里仅仅体现数值即可
- int weight1=180;
- int weight2=200;
- 用三元运算符实现老虎体重的判断,体重相同,返回true,否则,返回false
- (weight1==weight2)? true:false;
- 输出结果
public class 两只老虎 {
public static void main(String[] args) {
//1. 定义两个变量用于保存老虎的体重,单位为kg,这里仅仅体现数值即可
int weight1 = 180;
int weight2 = 200;
//2. 用三元运算符实现老虎体重的判断,体重相同,返回true,否则,返回false
boolean b = weight1 == weight2 ? true : false;
//3. 输出结果
System.out.println("b:" + b);
}
}
6.3 案例:三个和尚
- 需求:比较三个和尚的身高,150cm,210cm,165cm,请用程序实现获取这三个和尚的最高身高。
- 分析
- 定义三个变量用于保存和尚的身高,单位为cm,这里仅仅体现数值即可
- 用三元运算符获取前两个和尚的较高身高值,并用临时身高变量保存起来
- 用三元运算符获取临时身高值和第三个和尚的身高较高值,并用最大身高变量保存
- 输出结果
/*
三个和尚
需求:
比较三个和尚的身高150,210,165
*/
public class 三个和尚 {
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);
}
}
需求:
比较三个和尚的身高150,210,165
*/
public class 三个和尚 {
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);
}
}