1. 算术运算符
-
+ - * /四则运算 -
其中
/要注意,如果没有浮点数参与会得到一个整数结果,不是小数,这点和C/C++是一样的,不同于JavaScript -
0和0.0的除法结果是 NAN
-
非0和0.0的除法结果是无穷大
Infinity -
余数符号取决于左操作数
-
+连接运算符, 只要+两边的操作数中有一个操作数是字符串类型,则该+就被当做字符串连接符处理
package task01.day03;
import java.util.Scanner;
public class C01Aritch {
public static void main(String[] args) {
// 算术运算 和 C语言没有差别
System.out.println(5 + 8);
System.out.println(5 - 8);
System.out.println(5 * 8);
System.out.println(5 / 8); // 0
// 除法要保留小数
System.out.println(5 * 1.0 / 8); // 0.625
// 除数不能是0
// java.lang.ArithmeticException: / by zero
// System.out.println(1 / 0);
System.out.println(0 / 0.0); // NaN
System.out.println(1 / 0.0); // 无穷大 Infinity
// 余数符号取决于左操作数
System.out.println(15 % 4);
System.out.println(-15 % -4);
System.out.println(-15 % 4);
System.out.println(15 % -4);
// 练习:
// 提示用户输入正整数类型的秒数,拆分秒数后输出x小时x分x秒
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个秒数: ");
int time = scanner.nextInt();
int hour = time / 3600;
int minutes = time % 3600 / 60;
int seconds = time % 60;
System.out.println("时间: " + hour + "时" + minutes + "分" + seconds + "秒");
scanner.close();
// + 连接运算符
// 只要+两边的操作数中有一个操作数是字符串类型,则该+就被当做字符串连接符处理
System.out.println(3 + 5 + "hello");
System.out.println("hello" + 3 + 5);
System.out.println("hello" + (3 + 5));
}
}
2. 比较运算符
-
大于 >, 小于 <, 大于等于 >=, 小于等于 <=
-
相等 ==, 不同 !=
package task01.day03;
import java.util.Scanner;
public class C02Relation {
public static void main(String[] args) {
int x = 100;
int y = 20;
System.out.println(x != y);
System.out.println(x == y);
System.out.println(x >= y);
System.out.println(x > y);
System.out.println(x <= y);
System.out.println(x < y);
// 练习,判断一个数为负数
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数: ");
int num = scanner.nextInt();
boolean res = num < 0;
System.out.println( num + "是负数: " + res);
}
}
3. 自增与自减运算符
- 只能用于变量,常数不可以
- 前自增和后自增的区别
- 前自增先自增,再将自增后的值,作为表达式的值。
- 后自增是先将当前的值作为表达式的值,再自增。
package task01.day03;
public class C03Incre {
public static void main(String[] args) {
// 只能用于变量,常数不可以
int x = 10;
int res = ++x;
System.out.println("x = " + x + ", res = " + res); // 11, 11
x = 10;
res = x++;
System.out.println("x = " + x + ", res = " + res); // 11, 10
x = 10;
res = x--;
System.out.println("x = " + x + ", res = " + res); // 9, 10
x = 10;
res = --x;
System.out.println("x = " + x + ", res = " + res); // 9, 9
// 练习
x = 10;
res = x++ + ++x; // 表达式10/变量11 + 表达式12/变量12
System.out.println("x = " + x + ", res = " + res); // 12, 22
}
}
4. 逻辑运算符
- 运算结果是布尔类型
- && 有假为假,全真为真。
- || 有真为真,全假为假。
- ! 真为假,假为真。
- 短路特性
- && :如果第一个表达式为假,则第二个表达式不进行运算。
- || :如果第一个表达工为真,则第二个表达式不进行运算。
package task01.day03;
import java.util.Scanner;
public class C04Logic {
public static void main(String[] args) {
// &&
boolean b1 = true;
boolean b2 = false;
System.out.println(!b1);
System.out.println(!b2);
// &&
System.out.println(b1 && (100 >= 100));
System.out.println(b2 &&( 0 == 0));
// ||
System.out.println(b1 || (1 > 100));
System.out.println(b2 || (0 == 0));
// 短路特性
// 对于逻辑与运算符来说,若第一个条件为假则整个表达式为假,此时跳过第二个表达式不执行
int num = 100;
boolean res = b2 && (++num > 0);
System.out.print("num = " + num);
// 对于逻辑或运算符来说,若第一个条件为真则整个表达式为真,此时跳过第二个表达式不执行
res = b1 || (num-- < 0);
System.out.println("num = " + num);
// 练习 提示用户输入一个正整数,使用逻辑运算符判断该正整数是否为三位数,
//若是则打印true,否则打印false
System.out.println("请输入一个正整数: ");
Scanner scanner = new Scanner(System.in);
int data = scanner.nextInt();
System.out.println(data + "是否为三位数: " + (data >= 100 && data < 1000) );
}
}
5. 三目表达式
- 运算表达式 ? 表达式1 : 表达式2
- 如果运算表达式的结果为真,则返回表达式1的结果作为三目运算符的结果
- 如果运算表达式的结果为假,则返回表达式2的结果作为三目运算符的结果。
package task01.day03;
import java.util.Scanner;
public class C05Three {
public static void main(String[] args) {
// 三目运算
// 提示用户输入两个整数,使用三目运算符找到最大值并打印出来
System.out.println("输入两个整数: ");
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
scanner.close();
System.out.println("最大值是:" + (x > y ? x : y));
}
}
6. 赋值与赋合赋值
- = 赋值
- += -= *= /= %= 等
package task01.day03;
public class C06Assign {
public static void main(String[] args) {
// 赋值
int a = 100;
int x, y, z;
x = y = z = 300;
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
// 复合赋赋值
x += 20;
System.out.println("x =" + x);
x /= 5;
System.out.println("x =" + x);
// 细节注意点
byte b = 100;
// b = b + 2; // 错误,因为 b + 2(int) 会转换为in
b = (byte)(b + 2);
b += 30; // 相当于 b = (byte)(b + 30);
System.out.println(b);
}
}
7. 位运算
-
位运算是基于二进制进行计算
-
& 按位与,同1为1,否则为0
-
| 按位或,有1得1,否则为0
-
~ 按位取反,有1得0, 有0得1
-
^ 按位异或,相同为0,不同为1
-
<<左移运算,左移n位,相当于乘以2的n次方 -
>>可移运算,右移n位,相当于除以2的次方, 使用符号位填充左边的空位 -
>>>无符号右移,和右移相同运算,不同的是,它使用0来填充左边的空位。
package task01.day03;
public class C07Bit {
public static void main(String[] args) {
// 位运算
// 左移 相当于当前整数的数值乘以 2^n
System.out.println( 2 << 2); // 8
// 右移 相当于当前整数的数值除以 2^n, 左边填充符号位
System.out.println( 64 >> 4); // 4
// 逻辑右移 左边填0
System.out.println(-64 >> 4); // -4
// 逻辑右移 左边填0
System.out.println(-1 >>> 1); // 2147483647
// 位运算
/**
* 3: 0000 0011
* 4: 0000 0100 -> 1111 1011 -> 0000 0101 -> -5
* 5: 0000 0101
*/
System.out.println(~4); // -5
System.out.println(3 | 5); // 0000 0111 -> 7
System.out.println(3 & 5); // 0000 0001 -> 1
System.out.println(3 ^ 5); // 0000 0110 -> 6
}
}
8. 运算符优先级
- () 的优先级最高
- 当不确定运算符优先级时,直接使用()包起来。