| 一元运算符! | 二元运算符 | 三元运算符? |
| 赋值运算符 | 算术运算符 |
| 关系运算符 | 逻辑运算符 |
| 三元运算符 | 位运算符 |
class Precious {
public static void main(String[] args) {
byte a = 10;
byte b = 20;
byte res = (byte) (a + b);
System.out.println(res);
}
}
class Precious {
public static void main(String[] args) {
byte b = 20;
byte res = 0;
res += b;
System.out.println(res);
}
}
class Arithmetic {
public static void main(String[] args) {
int a = 5;
int b = 3;
System.out.printf("%d + %d = %d\n", a, b, a + b);
System.out.printf("%d - %d = %d\n", a, b, a - b);
System.out.printf("%d * %d = %d\n", a, b, a * b);
System.out.printf("%d / %d = %d\n", a, b, a / b);
System.out.printf("%d %% %d = %d\n", a, b, a % b);
}
}
class IncDec {
public static void main(String[] args) {
int a = 3;
a++;
int b = 3;
++b;
System.out.println(a);//4
System.out.println(b);//4
int c = 3;
System.out.println(c++);//3
int d = 3;
System.out.println(++d);//4
}
}
class Inverse {
public static void main(String[] args) {
int a = 6789;
System.out.println(a);
int unit = a % 10;
int ten = a / 10 % 10;
int hund = a / 100 % 10;
int thou = a / 1000 % 10;
System.out.println(unit + "-" + ten + "-" + hund + "-" + thou);
System.out.println(unit * 1000 + ten * 100 + hund * 10 + thou);
}
}
class Logical {
public static void main(String[] args) {
int a = 10;
int b = 5;
boolean bool = a++ > 10 && b-- < 20;
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("bool=" + bool);
}
}
class Ternary {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println("max=" + (a > b ? a : b));
System.out.println("min=" + (a < b ? a : b));
double res0 = a > b ? 10.0 : 2;
int res1 = a > b ? 10 : 20.0;//Type mismatch: cannot convert from double to int
int res2 = a < b ? 10.0 : 20;//Type mismatch: cannot convert from double to int
}
}
| & | 结果 |
| 0 & 0 | 0 |
| 0 & 1 | 0 |
| 1 & 0 | 0 |
| 1 & 1 | 1 |
| | | 结果 |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| ^ | 结果 |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| ~ | 结果 |
| ~0 | 1 |
| ~1 | 0 |
class Bit {
public static void main(String[] args) {
int a = 255;
int b = 15;
// 都是补码;正数的原码和补码相同
// 0000 0000 0000 0000 0000 0000 1111 1111 a
// 0000 0000 0000 0000 0000 0000 0000 1111 b
// 0000 0000 0000 0000 0000 0000 0000 1111 a & b
System.out.println("res=" + (a & b));
int c = 254;
// 0000 0000 0000 0000 0000 0000 1111 1110 正数原码=反码=补码
// 取反后
// 1111 1111 1111 1111 1111 1111 0000 0001
// 结果调整[取反后符号为1,是负数]
// 1111 1111 1111 1111 1111 1111 0000 0000 反码=补码-1
// 1000 0000 0000 0000 0000 0000 1111 1111 原码=反码取反[除符号位外]
System.out.println("res=" + (~c));
int d = -245;
// 1000 0000 0000 0000 0000 0000 1111 0101 原码[第一位是符号位,正数为0;负数为1]
// 1111 1111 1111 1111 1111 1111 0000 1010 反码=原码取反[除符号位外]
// 1111 1111 1111 1111 1111 1111 0000 1011 补码=反码+1
// 0000 0000 0000 0000 0000 0000 1111 0100 ~[取反后符号为0,是正数]
System.out.println("res=" + (~d));
}
}
class LeftMove {
public static void main(String[] args) {
System.out.println("res=" + (10 << 2));//40
// 1000 0000 0000 0000 0000 0000 0000 1010 原码
// 1111 1111 1111 1111 1111 1111 1111 0101 反码
// 1111 1111 1111 1111 1111 1111 1111 0110 补码
// 11 1111 1111 1111 1111 1111 1111 0110 00 <<2
// 11 1111 1111 1111 1111 1111 1111 0101 11 反码
// 10 0000 0000 0000 0000 0000 0000 1010 00 原码
System.out.println("res=" + (-10 << 2));//-40
}
}