算数操作符
基本的有:+ - * / %
自增自减:++ --
1. 基本算数操作符
+ - * /
public class HelloWorld {
public static void main(String[] args) {
int i = 10;
int j = 5;
int a = i+j;
int b = i - j;
int c = i*j;
int d = i /j;
}
}
2. 练习 - 求和
使用 Scanner 从控制台获取两个数字,然后计算这两个数字的和
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println("第一个整数:" + a);
int b = sc.nextInt();
System.out.println("第二个整数:" + b);
int c = a + b;
System.out.println("两个数的和是:" + c);
}
}
3. 任意运算单元的长度超过int
如果任何运算单元的长度都不超过int,那么运算结果就按照int来计算
byte a = 1;
byte b = 2;
a+b - int 类型
public class HelloWorld {
public static void main(String[] args) {
byte a = 1;
byte b = 2;
byte c = (byte) (a + b); // 虽然ab都是byte类型,但是运算结果是int类型,需要进行强制转换
int d = a + b;
}
}
4. %取模
% 取余数,又叫取模
5除以2,余1
public class HelloWorld {
public static void main(String[] args) {
int i = 5;
int j = 2;
System.out.println(i % j); // 输出为1
}
}
5. 自增 自减
++、-- :在原来的基础上增加1或者减少1
public class HelloWorld {
public static void main(String[] args) {
int i = 5;
i++;
System.out.println(i); // 输出为6
}
}
6. 自增 自减操作符置前以及置后的区别
以++为例
int i = 5;
i++; 先取值,再运算
++i; 先运算,再取值
public class HelloWorld {
public static void main(String[] args) {
int i = 5;
System.out.println(i++); // 5
System.out.println(i); // 6
int j = 5;
System.out.println(++j); // 6
System.out.println(j); // 6
}
}
7. 练习 - 自增
public class HelloWorld {
public static void main(String[] args) {
int i = 1;
int j = ++i + i++ + ++i + ++i + i++;
System.out.println(j); // 18
}
}
8. 练习 - BMI
使用Scanner收集你的身高体重,并计算出你的BMI值是多少
BMI的计算公式是 体重(kg) / (身高*身高)
比如邱阳波的体重是72kg, 身高是1.69,那么这位同学的BMI就是
72 / (1.69*1.69) = ?
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入身高(m):");
float height = sc.nextFloat();
System.out.println("请输入体重(kg):");
float weight = sc.nextFloat();
float BMI = weight / (height * height);
System.out.println("当前的BMI是:" + BMI);
}
}
关系操作符
关系操作符:比较两个变量之间的关系
1. 关系操作符
大于 >
大于或等于 >=
< 小于
<= 小于或等于
== 是否相等
!= 是否不等
public class HelloWorld {
public static void main(String[] args) {
int a = 5;
int b = 6;
int c = 5;
System.out.println(a > b); // 返回 false
System.out.println(a >= c); // 返回 true
System.out.println(a == b); // 返回false
System.out.println(a != b);// 返回true
}
}
2. 练习 - 关系操作符
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println("第一个整数:" + a);
int b = sc.nextInt();
System.out.println("第二个整数:" + b);
System.out.println("比较" + a + ">" + b + ":" + (a > b));
System.out.println("比较" + a + ">=" + b + ":" + (a >= b));
System.out.println("比较" + a + "<" + b + ":" + (a < b));
System.out.println("比较" + a + "<=" + b + ":" + (a <= b));
System.out.println("比较" + a + "==" + b + ":" + (a == b));
System.out.println("比较" + a + "!=" + b + ":" + (a != b));
}
}
逻辑运算符
1. 长路与 和 短路与
无论长路与还是短路与
两边的运算单元都是布尔值
都为真时,才为真
任意为假,就为假
区别
长路与 两侧,都会被运算
短路与 只要第一个是false,第二个就不进行运算了
public class HelloWorld {
public static void main(String[] args) {
// 长路与 无论第一个表达式的值是true或者false,第二个的值,都会被运算
int i = 2;
System.out.println(i == 1 & i++ == 2); // 无论如何i++都会被执行,所以i的值变成了3
System.out.println(i);
// 短路与 只要第一个表达式的值是false的,第二个表达式的值,就不需要进行运算了
int j = 2;
System.out.println(j == 1 && j++ == 2); // 因为j==1返回false,所以右边的j++就没有执行了,所以j的值,还是2
System.out.println(j);
}
}
false
3
false
2
2. 长路或 和 短路或
无论长路或还是短路或
两边的运算单元都是布尔值
都为假时,才为假
任意为真,就为真
区别
长路或 两侧都会被运算
短路或 只要第一个是true的,第二个就不进行运算了
public class HelloWorld {
public static void main(String[] args) {
// 长路或 无论第一个表达式的值是true或者false,第二个的值,都会被运算
int i = 2;
System.out.println(i == 1 | i++ == 2); // 无论如何i++都会被执行,所以i的值变成了3
System.out.println(i);
// 短路或 只要第一个表达式的值是true的,第二个表达式的值,就不需要进行运算了
int j = 2;
System.out.println(j == 2 || j++ == 2); // 因为j==2返回true,所以右边的j++就没有执行了,所以j的值,还是2
System.out.println(j);
}
}
true
3
true
2
3. 取反
取反!
真变为假
假变为真
public class HelloWorld {
public static void main(String[] args) {
boolean b = true;
System.out.println(b); // 输出true
System.out.println(!b);// 输出false
}
}
4. 异或^
异或^
不同,返回真
相同,返回假
public class HelloWorld {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println(a ^ b); // 不同返回真
System.out.println(a ^ !b); // 相同返回假
}
}
5. 练习 - 逻辑操作符
public class HelloWorld {
public static void main(String[] args) {
int i = 1;
boolean b = !(i++ == 3) ^ (i++ == 2) && (i++ == 3);
System.out.println(b); // false
System.out.println(i); // 3
}
}
位操作符
位操作符在实际工作中用的 并不常见
1. 一个整数的二进制表达
位操作都是对二进制而言的,但是我们平常使用的都是十进制比如5。
而5的二进制是101。
所以在开始学习之前,需要掌握一个整数的二进制表达是多少。
通过Integer.toBinaryString() 方法,将一个十进制整数转换为一个二进制字符串
public class HelloWorld {
public static void main(String[] args) {
int i = 5;
String b = (Integer.toBinaryString(i));// 5的二进制的表达101
System.out.println(i + "的二进制表达是: " + b);
}
}
2. 位或
5的二进制是101
6的二进制是110
所以 5|6 对每一位进行或运算,得到 111 - 7
public class HelloWorld {
public static void main(String[] args) {
int i = 5;
int j = 6;
System.out.println(Integer.toBinaryString(i)); // 5的二进制是101
System.out.println(Integer.toBinaryString(j)); // 6的二进制是110
System.out.println(i | j); // 所以 5|6 对每一位进行或运算,得到 111->7
}
}
3. 位与
5的二进制是101
6的二进制是110
所以 5&6 对每一位进行与运算,得到 100 - 4
public class HelloWorld {
public static void main(String[] args) {
int i = 5;
int j = 6;
System.out.println(Integer.toBinaryString(i)); // 5的二进制是101
System.out.println(Integer.toBinaryString(j)); // 6的二进制是110
System.out.println(i & j); // 所以 5&6 对每一位进行与运算,得到 100->4
}
}
4. 异或
5的二进制是101
6的二进制是110
所以 5^6 对每一位进行异或运算,得到 011->3
一些特别情况:
任何数和自己进行异或 都等于 0
任何数和0 进行异或 都等于自己
public class HelloWorld {
public static void main(String[] args) {
int i = 5;
int j = 6;
System.out.println(Integer.toBinaryString(i)); // 5的二进制是 101
System.out.println(Integer.toBinaryString(j)); // 6的二进制是110
System.out.println(i ^ j); // 所以 5^6 对每一位进行或运算,得到 011->3
System.out.println(i ^ 0); // 5
System.out.println(i ^ i); // 0
}
}
5. 取非
5 的二进制是 00000101
所以取反即为 11111010
这个二进制换算成十进制即为-6
public class HelloWorld {
public static void main(String[] args) {
byte i = 5;
System.out.println(Integer.toBinaryString(i)); // 5的二进制是00000101,所以取非即为11111010,即为-6
System.out.println(~i); // -6
}
}
6. 左移 右移
左移:根据一个整数的二进制表达,将其每一位都向左移动,最右边一位补0
右移:根据一个整数的二进制表达,将其每一位都向右移动
public class HelloWorld {
public static void main(String[] args) {
byte i = 6;
// 6的二进制是110
System.out.println(Integer.toBinaryString(i)); // 110
// 6向左移1位后,变成1100,对应的10进制是12
System.out.println(i << 1); // 12
// 6向右移1位后,变成11,对应的10进制是3
System.out.println(i >> 1); // 3
}
}
7. 练习 - 快速计算2x16
不用乘法符号(*) 计算 2x16
public class HelloWorld {
public static void main(String[] args) {
int i = 2;
int j = 2 * 16;
System.out.println("j=2*16:\t" + j);
int k = 2 << 4; // 1 << 5,4 << 3,8 << 2
System.out.println("2<<4:\t" + k);
}
}
8. 带符号右移与无符号右移
带符号右移 >>
对于正数, 带符号右移 >> 会把所有的位右移,并在最前面补0
对于负数, 带符号右移 >> 会把所有的位右移,并在最前面补1
无符号右移>>>
如果是一个负数,那么对应的二进制的第一位是1
无符号右移>>>会把第一位的1也向右移动,导致移动后,第一位变成0
这样就会使得负数在无符号右移后,得到一个正数
简单的说:
带符号右移 >> 移动后正的还是正的,负的还是负的,符号不变
无符号右移>>> 移动后,变正的了
public class HelloWorld {
public static void main(String[] args) {
int i = -10;
// -10的二进制是11111111111111111111111111110110
// 第一位是1,即符号位,代表这是一个负数
System.out.println(Integer.toBinaryString(i));
// 对于正数, 带符号右移 >> 会把所有的位右移,并在最前面补0
// 对于负数, 带符号右移 >> 会把所有的位右移,并在最前面补1
// -10带符号右移1位,移动后前面补齐1
// 得到11111111111111111111111111111011
// 因为第一位是1,所以依然是一个负数,对应的十进制是-5
int j = i >> 1;
System.out.println(Integer.toBinaryString(j));
System.out.println(j); // -5
// -10无符号向右移1位,符号位也会向右移,第一位就变成了0
// 得到01111111111111111111111111111011,对应的十进制是2147483643
int k = i >>> 1;
System.out.println(Integer.toBinaryString(k));
System.out.println(k);
}
}
9. 练习 - 位操作符
public class HelloWorld {
public static void main(String[] args) {
int i = 3;
int j = 2;
int c = ((i | j) ^ (i & j)) << 2 >>> 1;
// int c = ((i | j) ^ (i & j)) << 2 >> 1;
System.out.println(c); // 2
}
}
赋值操作符
1. 赋值操作
赋值操作的操作顺序是从右到左
int i = 5+5;
首先进行5+5的运算,得到结果10,然后把10这个值,赋给i
public class HelloWorld {
public static void main(String[] args) {
int i = 5 + 5;
}
}
2. 对本身进行运算,并赋值
+=即自加
i+=2;
等同于
i=i+2;
其他的 -= , *= , /= , %= , &= , |= , ^= , >>= , >>>= 都是类似,不做赘述
public class HelloWorld {
public static void main(String[] args) {
int i = 3;
i += 2;
System.out.println(i); // 5
int j = 3;
j = j + 2;
System.out.println(j); // 5
}
}
3. 练习 - 赋值操作符
public class HelloWorld {
public static void main(String[] args) {
int i = 1;
i += ++i;
System.out.println(i); // 3
}
}
三元操作符
1. 三元操作符
表达式?值1:值2
如果表达式为真 返回值1
如果表达式为假 返回值2
public class HelloWorld {
public static void main(String[] args) {
int i = 5;
int j = 6;
int k = i < j ? 99 : 88;
// 相当于
if (i < j) {
k = 99;
} else {
k = 88;
}
System.out.println(k);
}
}
2. 练习 - 判断是否是工作日
通过Scanner输入一个1-7之间的整数,使用三元操作符判断是工作日还是周末?
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("今天是周几?");
int day = sc.nextInt();
String week = (day <= 7 && day >= 1) ? (day <= 5 ? "今天是工作日" : "今天是周末") : "请输入有效日期";
System.out.println(week);
}
}
Scanner
1. 使用Scanner读取整数
注意: 使用Scanner类,需要在最前面加上
import java.util.Scanner;
表示导入这个类,才能够正常使用
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println("第一个整数:" + a);
int b = sc.nextInt();
System.out.println("第二个整数:" + b);
}
}
2. 使用Scanner读取浮点数
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float a = sc.nextFloat();
System.out.println("读取的浮点数的值是:" + a);
}
}
3. 使用Scanner读取字符串
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
System.out.println("读取的字符串是:" + a);
}
}
4. 读取了整数后,接着读取字符串
需要注意的是,如果在通过nextInt()读取了整数后,再接着读取字符串,读出来的是回车换行:"\r\n",因为nextInt仅仅读取数字信息,而不会读取回车换行"\r\n"。
所以,如果在业务上需要读取了整数后,接着读取字符串,那么就应该连续执行两次nextLine(),第一次是取走回车换行,第二次才是读取真正的字符串
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt(); // 123
System.out.println("读取的整数是:" + i); // 123
String s = sc.next(); // 123 123
System.out.println(s); // 123
String rn = sc.nextLine();
String a = sc.nextLine(); // 123 123 123
System.out.println("读取的字符串是:" + a); // 123 123 123
}
}