JAVA运算符详解
一、运算符分类概览
Java运算符分为7大类:
- 算术运算符:+ - * / % ++ --
- 关系运算符:== != > < >= <=
- 逻辑运算符:&& || ! & | ^
- 位运算符:& | ^ ~ << >> >>>
- 赋值运算符:= += -= *= /= %=
- 条件运算符:? :
- 其他运算符:instanceof、箭头运算符->(Lambda)
二、算术运算符详解
1. 基础四则运算
public class BasicMath {public static void main(String[] args) {int a = 10;int b = 3;System.out.println(a + b);  // 13(加法)System.out.println(a - b);  // 7(减法)System.out.println(a * b);  // 30(乘法)System.out.println(a / b);  // 3(整数除法,舍去小数)System.out.println(a % b);  // 1(取余)// 浮点数运算double x = 5.0;double y = 2.0;System.out.println(x / y);  // 2.5(精确除法)}
}
重要特性:
- 整数除法会丢弃小数部分
- %取余结果符号与被除数相同(- -10%3 = -1)
- +可连接字符串:- "Hello" + 123 → "Hello123"
2. 自增自减运算符
public class IncrementDemo {public static void main(String[] args) {int num = 5;// 前自增:先加1再使用System.out.println(++num);  // 6// 后自增:先使用再加1System.out.println(num++);  // 6(此时num=7)int a = 10;int b = a++ + ++a;  // 10 + 12 = 22System.out.println(b); }
}
易错点:
- 避免在复杂表达式中混合使用(如i++ + ++i)
- 单独使用时i++与++i效果相同
三、关系运算符
用于比较两个值的关系,返回boolean类型结果:
public class Comparison {public static void main(String[] args) {int m = 10;int n = 20;System.out.println(m == n);  // falseSystem.out.println(m != n);  // trueSystem.out.println(m > n);   // falseSystem.out.println(m <= n);  // true// 浮点数比较陷阱double d1 = 0.1 + 0.2;double d2 = 0.3;System.out.println(d1 == d2);  // false(应判断差值是否<1e-6)}
}
四、逻辑运算符
1. 基础逻辑运算
public class LogicDemo {public static void main(String[] args) {boolean a = true;boolean b = false;System.out.println(a && b);  // false(逻辑与)System.out.println(a || b);  // true(逻辑或)System.out.println(!a);      // false(逻辑非)}
}
2. 短路特性(重要!)
public class ShortCircuit {public static void main(String[] args) {int x = 5;int y = 10;// 当第一个条件为false时,&&不再执行第二个判断if (x > 10 && y++ < 20) { // 不会执行}System.out.println(y);  // 10(y未自增)// 位运算符&会执行全部判断if (x > 10 & y++ < 20) { // 不会执行,但y仍自增}System.out.println(y);  // 11}
}
关键区别:
- &&和- ||具有短路特性
- &和- |无论结果如何都会执行全部表达式
五、位运算符(硬件级操作)
public class BitwiseOps {public static void main(String[] args) {int a = 5;    // 二进制 0101int b = 3;    // 二进制 0011System.out.println(a & b);   // 0001 → 1(按位与)System.out.println(a | b);    // 0111 → 7(按位或)System.out.println(a ^ b);   // 0110 → 6(异或:相同为0)System.out.println(~a);       // 1010 → -6(取反)// 移位运算int num = 8;  // 二进制 1000System.out.println(num << 2);  // 32(左移2位,相当于*4)System.out.println(num >> 1);  // 4(带符号右移,相当于/2)System.out.println(-8 >>> 1);  // 2147483644(无符号右移)}
}
应用场景:
- 权限系统(用位表示权限)
- 加密算法
- 高性能计算
六、赋值运算符
public class Assignment {public static void main(String[] args) {int x = 10;x += 5;    // 等价于 x = x + 5 → 15x *= 2;    // x = 15 * 2 → 30// 特殊案例byte b = 5;b += 10;   // 正确(自动强制转换)// b = b + 10;  // 错误!需要强制转换}
}
复合赋值特点:
- 自动强制类型转换
- 比普通赋值更高效
七、条件运算符(三元运算符)
public class InstanceofDemo {public static void main(String[] args) {String str = "Hello";System.out.println(str instanceof String);  // trueSystem.out.println(str instanceof Object); // true(所有类都是Object子类)}
}
最佳实践:
- 替代简单if-else
- 避免多层嵌套
八、其他运算符
1. instanceof 类型判断
public class InstanceofDemo {public static void main(String[] args) {String str = "Hello";System.out.println(str instanceof String);  // trueSystem.out.println(str instanceof Object); // true(所有类都是Object子类)}
}
2. Lambda箭头运算符(Java 8+)
import java.util.function.Consumer;public class LambdaDemo {public static void main(String[] args) {Consumer<String> printer = s -> System.out.println(s);printer.accept("Hello Lambda!");}
}
九、运算符优先级表(部分)
| 优先级 | 运算符 | 
|---|---|
| 1 | ()[].(方法调用) | 
| 2 | !~++--(单目运算) | 
| 3 | */% | 
| 4 | +- | 
| 5 | <<>>>>> | 
| 6 | <<=>>=instanceof | 
| 7 | ==!= | 
| 8 | & | 
| 9 | ^ | 
| 10 | ` | 
| 11 | && | 
| 12 | ` | 
| 13 | ? : | 
| 14 | =+=-=等赋值运算符 | 
记忆技巧:
- 单目 > 算术 > 移位 > 关系 > 逻辑 > 条件 > 赋值
- 不确定时使用括号明确优先级
十、综合练习
public class OperatorQuiz {public static void main(String[] args) {int a = 5, b = 3;boolean flag = (a++ > 5) && (++b < 5);System.out.println("a=" + a + ", b=" + b);  // a=6, b=3(短路特性)int x = 4, y = 8;int max = (x > y) ? x : y;System.out.println("较大值:" + max);  // 8// 位运算交换变量x = x ^ y;y = x ^ y;x = x ^ y;System.out.println("x=" + x + ", y=" + y);  // x=8, y=4}
}
十一、常见错误集锦
整数除以零:int a = 5 / 0;  // 运行时抛出ArithmeticException
浮点数相等判断:if (0.1 + 0.2 == 0.3) {  // false!应判断差值// 不会执行
}
自增运算符混淆:int i = 5;
int j = i++ + ++i;  // 5 + 7 = 12(可读性差)
位运算优先级:int result = 5 & 3 == 1;  // 错误!等价于5 & (3 ==1)总结:
- Java所有运算符的分类和使用场景
- 运算符的优先级和结合性
- 常见错误及解决方法
- 位运算的实际应用技巧
