本文简述Java中操作符的一些知识点,属于个人学习总结,能力有限,还望各位大神多多指点。
文章目录
Precedence
优先级由高到低排序。
Operators | Precedence |
---|---|
postfix | expr++ expr– |
unary | ++expr –expr +expr -expr ~ ! |
multiplicative | * / % |
additive | + - |
shift | << >> >>> |
relational | < > <= >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | || |
ternary | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
虽然定义了操作符之间的优先级,但是还是要加上括号以提高可读性。
Notes
一些需要注意的地方。
Difference between expr++ and ++expr
The same to expr-- and --expr
public class Test {
public static void main(String[] args) {
int i = 0;
// compare first, then increase
if (i++ > 0) {
System.out.println("true branch: " + i);
} else {
// go into this branch
// false branch: 1
System.out.println("false branch: " + i);
}
int j = 0;
// increase first, then compare
if (++j > 0) {
// go into this branch
// true branch: 1
System.out.println("true branch: " + j);
} else {
System.out.println("false branch: " + j);
}
}
}
Difference between && and & in case of condition check
The same to || and |
public class Test {
public static void main(String[] args) {
int i = 0;
int j = 0;
// the whole conditions check is false if the first condition is false, then the following conditions don't need be checked.
// so, j++ won't be executed actually.
// this is called short circuit.
if (i++ > 0 && j++ > 0) {
System.out.println("true branch: i = " + i + ", j = " + j);
} else {
// go into this branch
// false branch: i = 1, j = 0
System.out.println("false branch: i = " + i + ", j = " + j);
}
int m = 0;
int n = 0;
// all conditions will be evaluated.
if (m++ > 0 & n++ > 0) {
System.out.println("true branch: m = " + m + ", n = " + n);
} else {
// go into this branch
// false branch: m = 1, n = 1
System.out.println("false branch: m = " + m + ", n = " + n);
}
}
}
Don’t use bitwise move on byte, short, and char
public class Test {
public static void main(String[] args) {
int i = 111;
// 00000000000000000000000001101111
printInt(i);
// 00000000000000000000000000011011
printInt(i >> 2);
// 00000000000000000000000110111100
printInt(i << 2);
// 00000000000000000000000000011011
printInt(i >>> 2);
System.out.println();
int j = -111;
// 11111111111111111111111110010001
printInt(j);
// 11111111111111111111111111100100
printInt(j >> 2);
// 11111111111111111111111001000100
printInt(j << 2);
// 00111111111111111111111111100100
printInt(j >>> 2);
System.out.println();
byte m = 111;
// 00000000000000000000000001101111
printInt(m);
// 00000000000000000000000000011011
printInt(m >> 2);
// 00000000000000000000000110111100
printInt(m << 2);
// 00000000000000000000000000011011
printInt(m >>> 2);
System.out.println();
byte n = -111;
// 11111111111111111111111110010001
printInt(n);
// 11111111111111111111111111100100
printInt(n >> 2);
// 11111111111111111111111001000100
printInt(n << 2);
// 00111111111111111111111111100100
printInt(n >>> 2);
// pay more attention to the last sample
// char, byte, and short literals are converted to int before any calculations.
// so when work on bitwise move, it may not as you expected.
// in this sample, binary code is 10010001 for byte
// after >>> 2, expected binary code is 00100100
// actual binary code is 00111111111111111111111111100100
// someone suggests to cut the last 8 bits, i.e. 11100100
// it's clearly that they are different
}
private static void printInt(int i) {
String s = Integer.toBinaryString(i);
System.out.println("0".repeat(32 - s.length()) + s);
}
}
备注
本文所有代码在JDK 11上测试通过。