当前位置: 首页 > news >正文

Java 运算符:深度解析

前言

作为Java开发者,运算符是我们每天都会接触的基础元素。然而,很多开发者对运算符的理解仅停留在表面层次。本文将全面深入地剖析Java中的各类运算符,揭示其底层原理、使用技巧和最佳实践,帮助您成为真正的Java运算符专家。

一、算术运算符深度解析

算术运算符是Java中最基础的运算符类型,用于执行基本的数学运算。

1.1 基本算术运算符

运算符描述示例
+加法a + b
-减法a - b
*乘法a * b
/除法a / b
%取模a % b
++自增a++ 或 ++a
自减a-- 或 --a
整数与浮点数运算差异
public class ArithmeticOperators {public static void main(String[] args) {// 整数除法示例int intA = 5;int intB = 2;int intResult = intA / intB;  // 结果为2,整数除法会截断小数部分System.out.println("5 / 2 (整数) = " + intResult);// 浮点数除法示例double doubleA = 5.0;double doubleB = 2.0;double doubleResult = doubleA / doubleB;  // 结果为2.5,浮点数除法保留小数System.out.println("5.0 / 2.0 (浮点数) = " + doubleResult);// 混合运算示例double mixedResult = intA / doubleB;  // 整数与浮点数运算,结果为浮点数System.out.println("5 / 2.0 (混合) = " + mixedResult);}
}

输出结果:

5 / 2 (整数) = 2
5.0 / 2.0 (浮点数) = 2.5
5 / 2.0 (混合) = 2.5

1.2 溢出问题及解决方案

Java中的数值类型都有固定的大小,当运算结果超出该类型能表示的范围时,就会发生溢出。

public class OverflowExample {public static void main(String[] args) {// 整数溢出示例int maxInt = Integer.MAX_VALUE;  // 2147483647System.out.println("Max int: " + maxInt);int overflowInt = maxInt + 1;   // 溢出到最小值System.out.println("Max int + 1: " + overflowInt);// 浮点数溢出示例double maxDouble = Double.MAX_VALUE;System.out.println("Max double: " + maxDouble);double overflowDouble = maxDouble * 2;  // 浮点数溢出结果为无穷大System.out.println("Max double * 2: " + overflowDouble);// 避免溢出的方法// 方法1:使用更大的数据类型long bigger = (long)maxInt + 1;System.out.println("使用long避免溢出: " + bigger);// 方法2:使用Math类的安全运算方法int safeResult = Math.addExact(maxInt, 1);  // 会抛出ArithmeticException}
}

输出结果:

Max int: 2147483647
Max int + 1: -2147483648
Max double: 1.7976931348623157E308
Max double * 2: Infinity
使用long避免溢出: 2147483648
Exception in thread "main" java.lang.ArithmeticException: integer overflow

1.3 自增和自减运算符

public class IncrementDecrement {public static void main(String[] args) {// 前缀和后缀的区别int a = 5;int b = ++a;  // 前缀:先自增再赋值System.out.println("a = " + a + ", b = " + b);  // a=6, b=6int c = 5;int d = c++;  // 后缀:先赋值再自增System.out.println("c = " + c + ", d = " + d);  // c=6, d=5// 复杂表达式中的使用int x = 10;int y = x-- + --x;  // 10 (x=9) + 8 (x=8) = 18System.out.println("x = " + x + ", y = " + y);  // x=8, y=18}
}

输出结果:

a = 6, b = 6
c = 6, d = 5
x = 8, y = 18

二、关系运算符全面剖析

关系运算符用于比较两个值,返回布尔结果(true或false)。

2.1 基本关系运算符

运算符描述示例
==等于a == b
!=不等于a != b
>大于a > b
<小于a < b
>=大于等于a >= b
<=小于等于a <= b
public class RelationalOperators {public static void main(String[] args) {int a = 10;int b = 20;System.out.println("a == b: " + (a == b));  // falseSystem.out.println("a != b: " + (a != b));  // trueSystem.out.println("a > b: " + (a > b));    // falseSystem.out.println("a < b: " + (a < b));    // trueSystem.out.println("b >= a: " + (b >= a));  // trueSystem.out.println("b <= a: " + (b <= a));  // false// 浮点数比较的特殊性double x = 0.1 + 0.2;double y = 0.3;System.out.println("x == y: " + (x == y));  // false,浮点数精度问题System.out.println("Math.abs(x - y) < 1e-10: " + (Math.abs(x - y) < 1e-10));  // true,正确的浮点数比较方式}
}

输出结果:

a == b: false
a != b: true
a > b: false
a < b: true
b >= a: true
b <= a: false
x == y: false
Math.abs(x - y) < 1e-10: true

2.2 对象比较:== vs equals()

public class ObjectComparison {public static void main(String[] args) {String str1 = "Hello";String str2 = "Hello";String str3 = new String("Hello");String str4 = "World";// == 比较引用System.out.println("str1 == str2: " + (str1 == str2));  // true,字符串常量池System.out.println("str1 == str3: " + (str1 == str3));  // false,不同对象// equals() 比较内容System.out.println("str1.equals(str2): " + str1.equals(str2));  // trueSystem.out.println("str1.equals(str3): " + str1.equals(str3));  // trueSystem.out.println("str1.equals(str4): " + str1.equals(str4));  // false// 自定义对象的比较Person p1 = new Person("Alice", 25);Person p2 = new Person("Alice", 25);Person p3 = p1;System.out.println("p1 == p2: " + (p1 == p2));  // falseSystem.out.println("p1 == p3: " + (p1 == p3));  // trueSystem.out.println("p1.equals(p2): " + p1.equals(p2));  // true,假设已重写equals()}
}class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}@Overridepublic boolean equals(Object obj) {if (this == obj) return true;if (obj == null || getClass() != obj.getClass()) return false;Person person = (Person) obj;return age == person.age && name.equals(person.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}
}

输出结果:

str1 == str2: true
str1 == str3: false
str1.equals(str2): true
str1.equals(str3): true
str1.equals(str4): false
p1 == p2: false
p1 == p3: true
p1.equals(p2): true

三、逻辑运算符深入讲解

逻辑运算符用于组合多个布尔表达式,形成更复杂的逻辑判断。

3.1 基本逻辑运算符

运算符描述示例
&&逻辑与(短路)a && b
||逻辑或(短路)a || b
!逻辑非!a
&逻辑与(非短路)a & b
|逻辑或(非短路)a | b
^逻辑异或a ^ b
public class LogicalOperators {public static void main(String[] args) {boolean a = true;boolean b = false;// 基本逻辑运算System.out.println("a && b: " + (a && b));  // falseSystem.out.println("a || b: " + (a || b));  // trueSystem.out.println("!a: " + (!a));          // falseSystem.out.println("a ^ b: " + (a ^ b));     // true// 短路与非短路区别System.out.println("\n短路与非短路演示:");System.out.println("短路与(&&):");if (falseCondition() && trueCondition()) {  // 只执行第一个方法System.out.println("不会执行");}System.out.println("非短路与(&):");if (falseCondition() & trueCondition()) {   // 两个方法都会执行System.out.println("不会执行");}// 优先级示例System.out.println("\n优先级演示:");boolean result = true || false && false;  // && 优先级高于 ||System.out.println("true || false && false = " + result);  // true}static boolean trueCondition() {System.out.println("执行trueCondition");return true;}static boolean falseCondition() {System.out.println("执行falseCondition");return false;}
}

输出结果:

a && b: false
a || b: true
!a: false
a ^ b: true短路与非短路演示:
短路与(&&):
执行falseCondition
非短路与(&):
执行falseCondition
执行trueCondition优先级演示:
true || false && false = true

3.2 德摩根定律应用

德摩根定律是逻辑运算中的重要规则:

  • ¬(A ∧ B) = ¬A ∨ ¬B
  • ¬(A ∨ B) = ¬A ∧ ¬B
public class DeMorgansLaw {public static void main(String[] args) {boolean hasMoney = true;boolean hasTime = false;// 原始条件if (!(hasMoney && hasTime)) {System.out.println("你不能去旅行");}// 应用德摩根定律后if (!hasMoney || !hasTime)) {System.out.println("你不能去旅行(德摩根定律应用)");}// 另一个例子if (!(hasMoney || hasTime)) {System.out.println("你既没钱也没时间");}// 应用德摩根定律后if (!hasMoney && !hasTime)) {System.out.println("你既没钱也没时间(德摩根定律应用)");}}
}

输出结果:

你不能去旅行
你不能去旅行(德摩根定律应用)

四、位运算符全面分析

位运算符直接操作整数的二进制位,在底层编程和性能优化中非常有用。

4.1 基本位运算符

运算符描述示例
&按位与a & b
|按位或a | b
^按位异或a ^ b
~按位取反~a
<<左移a << b
>>右移(带符号)a >> b
>>>右移(无符号)a >>> b
public class BitwiseOperators {public static void main(String[] args) {int a = 5;    // 二进制: 0101int b = 3;    // 二进制: 0011// 基本位运算System.out.println("a & b: " + (a & b));   // 0001 = 1System.out.println("a | b: " + (a | b));   // 0111 = 7System.out.println("a ^ b: " + (a ^ b));   // 0110 = 6System.out.println("~a: " + (~a));         // 11111111111111111111111111111010 = -6// 移位运算System.out.println("a << 1: " + (a << 1)); // 1010 = 10System.out.println("a >> 1: " + (a >> 1)); // 0010 = 2System.out.println("-8 >> 1: " + (-8 >> 1)); // 11111111111111111111111111111100 = -4System.out.println("-8 >>> 1: " + (-8 >>> 1)); // 01111111111111111111111111111100 = 2147483644}
}

输出结果:

a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2
-8 >> 1: -4
-8 >>> 1: 2147483644

4.2 位运算应用案例

案例1:权限控制系统
public class PermissionSystem {// 定义权限常量public static final int READ = 1 << 0;    // 0001 = 1public static final int WRITE = 1 << 1;   // 0010 = 2public static final int EXECUTE = 1 << 2; // 0100 = 4public static void main(String[] args) {// 设置用户权限int userPermissions = READ | WRITE;  // 0011 = 3// 检查权限System.out.println("可以读取吗? " + ((userPermissions & READ) == READ));  // trueSystem.out.println("可以执行吗? " + ((userPermissions & EXECUTE) == EXECUTE));  // false// 添加权限userPermissions |= EXECUTE;  // 0111 = 7System.out.println("添加执行权限后可以执行吗? " + ((userPermissions & EXECUTE) == EXECUTE));  // true// 移除权限userPermissions &= ~WRITE;  // 0101 = 5System.out.println("移除写权限后可以写吗? " + ((userPermissions & WRITE) == WRITE));  // false}
}

输出结果:

可以读取吗? true
可以执行吗? false
添加执行权限后可以执行吗? true
移除写权限后可以写吗? false
案例2:快速计算
public class BitwiseTricks {public static void main(String[] args) {// 判断奇偶int num = 15;System.out.println(num + "是" + ((num & 1) == 0 ? "偶数" : "奇数"));  // 奇数// 交换两个数int x = 5, y = 10;x = x ^ y;y = x ^ y;x = x ^ y;System.out.println("交换后: x=" + x + ", y=" + y);  // x=10, y=5// 计算绝对值int n = -15;int abs = (n ^ (n >> 31)) - (n >> 31);System.out.println(n + "的绝对值是" + abs);  // 15// 判断是否为2的幂int power = 16;boolean isPowerOfTwo = (power & (power - 1)) == 0;System.out.println(power + (isPowerOfTwo ? "是" : "不是") + "2的幂");  // 是}
}

输出结果:

15是奇数
交换后: x=10, y=5
-15的绝对值是15
16是2的幂

五、赋值运算符详解

赋值运算符用于给变量赋值,Java提供了多种复合赋值运算符简化表达式。

5.1 基本赋值运算符

运算符描述示例等价于
=赋值a = ba = b
+=加后赋值a += ba = a + b
-=减后赋值a -= ba = a - b
*=乘后赋值a *= ba = a * b
/=除后赋值a /= ba = a / b
%=取模后赋值a %= ba = a % b
&=按位与后赋值a &= ba = a & b
|=按位或后赋值a |= ba = a | b
^=按位异或后赋值a ^= ba = a ^ b
<<=左移后赋值a <<= ba = a << b
>>=右移后赋值a >>= ba = a >> b
>>>=无符号右移后赋值a >>>= ba = a >>> b
public class AssignmentOperators {public static void main(String[] args) {// 基本赋值int a = 10;System.out.println("a = " + a);// 复合赋值a += 5;  // a = a + 5System.out.println("a += 5 → " + a);  // 15a -= 3;  // a = a - 3System.out.println("a -= 3 → " + a);  // 12a *= 2;  // a = a * 2System.out.println("a *= 2 → " + a);  // 24a /= 4;  // a = a / 4System.out.println("a /= 4 → " + a);  // 6a %= 4;  // a = a % 4System.out.println("a %= 4 → " + a);  // 2// 位运算复合赋值int b = 6;  // 0110b &= 3;     // 0010 (3=0011)System.out.println("b &= 3 → " + b);  // 2b |= 5;     // 0111 (5=0101)System.out.println("b |= 5 → " + b);  // 7b ^= 3;     // 0100 (3=0011)System.out.println("b ^= 3 → " + b);  // 4b <<= 1;    // 1000System.out.println("b <<= 1 → " + b);  // 8b >>= 2;    // 0010System.out.println("b >>= 2 → " + b);  // 2b >>>= 1;   // 0001System.out.println("b >>>= 1 → " + b);  // 1}
}

输出结果:

a = 10
a += 5 → 15
a -= 3 → 12
a *= 2 → 24
a /= 4 → 6
a %= 4 → 2
b &= 3 → 2
b |= 5 → 7
b ^= 3 → 4
b <<= 1 → 8
b >>= 2 → 2
b >>>= 1 → 1

5.2 复合赋值的类型转换特性

复合赋值运算符会自动进行隐式类型转换,而普通赋值可能需要显式转换。

public class CompoundAssignmentConversion {public static void main(String[] args) {// 普通赋值需要强制类型转换int x = 10;long y = 20;// x = x + y;  // 编译错误,需要强制转换x = (int)(x + y);System.out.println("x = " + x);  // 30// 复合赋值自动转换int a = 10;long b = 20;a += b;  // 自动转换为intSystem.out.println("a = " + a);  // 30// 另一个例子byte c = 10;int d = 5;// c = c + d;  // 编译错误c += d;  // 自动转换为byteSystem.out.println("c = " + c);  // 15}
}

输出结果:

x = 30
a = 30
c = 15

六、条件运算符(三元运算符)详解

条件运算符(?:)是Java中唯一的三元运算符,提供简洁的条件判断语法。

6.1 基本语法

condition ? expression1 : expression2

如果condition为true,返回expression1的值,否则返回expression2的值。

public class TernaryOperator {public static void main(String[] args) {int score = 85;String result = score >= 60 ? "及格" : "不及格";System.out.println("考试成绩: " + result);  // 及格// 嵌套条件运算符String grade = score >= 90 ? "优秀" : score >= 80 ? "良好" :score >= 70 ? "中等" :score >= 60 ? "及格" : "不及格";System.out.println("成绩等级: " + grade);  // 良好// 与if-else对比if (score >= 60) {result = "及格";} else {result = "不及格";}System.out.println("if-else结果: " + result);  // 及格}
}

输出结果:

考试成绩: 及格
成绩等级: 良好
if-else结果: 及格

6.2 条件运算符的特殊行为

public class TernaryBehavior {public static void main(String[] args) {// 类型转换行为Object o1 = true ? "字符串" : Integer.valueOf(1);System.out.println("o1的类型: " + o1.getClass().getName());  // StringObject o2 = false ? "字符串" : Integer.valueOf(1);System.out.println("o2的类型: " + o2.getClass().getName());  // Integer// 表达式求值行为int x = 10, y = 20;int max = (x > y) ? x++ : y++;System.out.println("max = " + max + ", x = " + x + ", y = " + y);  // max=20, x=10, y=21// 空指针安全String str = null;String display = str != null ? str : "默认值";System.out.println("显示: " + display);  // 默认值}
}

输出结果:

o1的类型: java.lang.String
o2的类型: java.lang.Integer
max = 20, x = 10, y = 21
显示: 默认值

七、instanceof运算符深度解析

instanceof运算符用于检查对象是否是特定类的实例或其子类的实例。

7.1 基本用法

public class InstanceofDemo {public static void main(String[] args) {Object obj = "Hello";System.out.println("obj instanceof String: " + (obj instanceof String));  // trueSystem.out.println("obj instanceof Object: " + (obj instanceof Object));  // trueSystem.out.println("obj instanceof Integer: " + (obj instanceof Integer));  // false// 与null的关系obj = null;System.out.println("null instanceof String: " + (obj instanceof String));  // false}
}

输出结果:

obj instanceof String: true
obj instanceof Object: true
obj instanceof Integer: false
null instanceof String: false

7.2 继承体系中的instanceof

class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}public class InstanceofInheritance {public static void main(String[] args) {Animal animal = new Dog();System.out.println("animal instanceof Animal: " + (animal instanceof Animal));  // trueSystem.out.println("animal instanceof Dog: " + (animal instanceof Dog));  // trueSystem.out.println("animal instanceof Cat: " + (animal instanceof Cat));  // false// 接口检查List<String> list = new ArrayList<>();System.out.println("list instanceof List: " + (list instanceof List));  // trueSystem.out.println("list instanceof ArrayList: " + (list instanceof ArrayList));  // trueSystem.out.println("list instanceof RandomAccess: " + (list instanceof RandomAccess));  // true}
}

输出结果:

animal instanceof Animal: true
animal instanceof Dog: true
animal instanceof Cat: false
list instanceof List: true
list instanceof ArrayList: true
list instanceof RandomAccess: true

7.3 实际应用案例

public class InstanceofApplication {public static void main(String[] args) {processObject("Hello");processObject(123);processObject(new ArrayList<>());processObject(null);}public static void processObject(Object obj) {if (obj instanceof String) {String s = (String) obj;System.out.println("处理字符串: " + s.toUpperCase());} else if (obj instanceof Integer) {Integer i = (Integer) obj;System.out.println("处理整数: " + (i * 2));} else if (obj instanceof List) {List<?> list = (List<?>) obj;System.out.println("处理列表,大小: " + list.size());} else {System.out.println("未知对象类型: " + obj);}}
}

输出结果:

处理字符串: HELLO
处理整数: 246
处理列表,大小: 0
未知对象类型: null

八、运算符优先级总结

Java运算符的优先级决定了表达式中运算的执行顺序。下表列出了从高到低的优先级:

优先级运算符描述结合性
1() [] .方法调用、数组下标、成员访问从左到右
2++ – ~ !自增、自减、按位取反、逻辑非从右到左
3* / %乘、除、取模从左到右
4+ -加、减从左到右
5<< >> >>>移位运算从左到右
6< > <= >= instanceof关系运算从左到右
7== !=相等性判断从左到右
8&按位与从左到右
9^按位异或从左到右
10|按位或从左到右
11&&逻辑与从左到右
12||逻辑或从左到右
13?:条件运算符从右到左
14= += -= *= /= %= &= ^= |= <<= >>= >>>=赋值运算从右到左
public class OperatorPrecedence {public static void main(String[] args) {int a = 10, b = 5, c = 1;int result = a + b * c;  // 乘法优先级高于加法System.out.println("a + b * c = " + result);  // 15boolean boolResult = a > b && b < c;  // 关系运算优先级高于逻辑运算System.out.println("a > b && b < c = " + boolResult);  // falseint bitResult = a & b | c;  // & 优先级高于 |System.out.println("a & b | c = " + bitResult);  // 1// 使用括号明确优先级int withParentheses = (a + b) * c;System.out.println("(a + b) * c = " + withParentheses);  // 15}
}

输出结果:

a + b * c = 15
a > b && b < c = false
a & b | c = 1
(a + b) * c = 15

九、性能优化与最佳实践

9.1 位运算优化技巧

操作常规写法位运算优化写法说明
乘以2x * 2x << 1左移1位相当于乘以2
除以2x / 2x >> 1右移1位相当于除以2
取模x % 8x & 7对2^n取模可用位与
判断奇偶x % 2 == 0(x & 1) == 0最后一位为0则是偶数
交换变量temp=a;a=b;b=temp;a^=b;b^=a;a^=b;不需要临时变量
绝对值Math.abs(x)(x ^ (x >> 31)) - (x >> 31)避免分支预测失败

9.2 运算符优先级表

Java运算符优先级从高到低:

优先级运算符类别结合性
1() [] .方法调用、数组访问左到右
2! ~ ++ -- + - (type)一元运算符右到左
3* / %乘除左到右
4+ -加减左到右
5<< >> >>>移位左到右
6< <= > >= instanceof关系左到右
7== !=相等左到右
8&位与左到右
9^位异或左到右
10``位或
11&&逻辑与左到右
12``
13?:三元右到左
14`= += -= *= /= %= &= ^== <<= >>= >>>=`赋值

9.3 常见陷阱与注意事项

  1. 整数溢出问题

    int max = Integer.MAX_VALUE;
    int result = max + 1;  // 溢出,结果为Integer.MIN_VALUE
    
  2. 浮点数比较

    // 错误方式
    if (a == b) {...}  // 浮点数不应直接比较// 正确方式
    if (Math.abs(a - b) < 1e-10) {...}
    
  3. 短路与非短路运算符

    // && 是短路与,如果第一个条件为false,不会计算第二个条件
    if (obj != null && obj.isValid()) {...}// & 是非短路与,两个条件都会计算
    if (obj != null & obj.isValid()) {...}  // 可能导致NullPointerException
    
  4. 位运算符号扩展

    byte b = -1;  // 0xFF
    int i = b;     // 0xFFFFFFFF (符号扩展)
    int j = b & 0xFF; // 0x000000FF (无符号扩展)
    
  5. 复合赋值运算符的类型转换

    byte b = 10;
    b = b + 1;      // 编译错误,需要强制转换
    b += 1;         // 正确,复合赋值包含隐式转换
    

十、综合案例分析

10.1 计算器实现

import java.util.Scanner;public class Calculator {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("简单计算器");System.out.println("请输入第一个数字:");double num1 = scanner.nextDouble();System.out.println("请输入运算符(+, -, *, /, %):");char operator = scanner.next().charAt(0);System.out.println("请输入第二个数字:");double num2 = scanner.nextDouble();double result = 0;boolean valid = true;// 使用switch处理不同运算符switch (operator) {case '+':result = num1 + num2;break;case '-':result = num1 - num2;break;case '*':result = num1 * num2;break;case '/':if (num2 != 0) {result = num1 / num2;} else {System.out.println("错误: 除数不能为零!");valid = false;}break;case '%':if (num2 != 0) {result = num1 % num2;} else {System.out.println("错误: 取模运算的除数不能为零!");valid = false;}break;default:System.out.println("错误: 不支持的运算符!");valid = false;}if (valid) {System.out.println("结果: " + num1 + " " + operator + " " + num2 + " = " + result);}scanner.close();}
}

10.2 权限管理系统

public class AdvancedPermissionSystem {// 权限常量public static final int VIEW = 1;       // 0001public static final int EDIT = 1 << 1;  // 0010public static final int DELETE = 1 << 2; // 0100public static final int ADMIN = 1 << 3;  // 1000public static void main(String[] args) {// 用户角色int guest = VIEW;int editor = VIEW | EDIT;int admin = VIEW | EDIT | DELETE | ADMIN;// 检查权限System.out.println("访客权限:");checkPermissions(guest);System.out.println("\n编辑权限:");checkPermissions(editor);System.out.println("\n管理员权限:");checkPermissions(admin);// 动态修改权限System.out.println("\n给编辑添加删除权限:");editor = addPermission(editor, DELETE);checkPermissions(editor);System.out.println("\n移除编辑的删除权限:");editor = removePermission(editor, DELETE);checkPermissions(editor);}// 检查权限public static void checkPermissions(int permissions) {System.out.println("可以查看: " + hasPermission(permissions, VIEW));System.out.println("可以编辑: " + hasPermission(permissions, EDIT));System.out.println("可以删除: " + hasPermission(permissions, DELETE));System.out.println("是管理员: " + hasPermission(permissions, ADMIN));System.out.println("权限值: " + Integer.toBinaryString(permissions));}// 检查是否有特定权限public static boolean hasPermission(int permissions, int permission) {return (permissions & permission) == permission;}// 添加权限public static int addPermission(int permissions, int permission) {return permissions | permission;}// 移除权限public static int removePermission(int permissions, int permission) {return permissions & ~permission;}
}

输出结果:

访客权限:
可以查看: true
可以编辑: false
可以删除: false
是管理员: false
权限值: 1编辑权限:
可以查看: true
可以编辑: true
可以删除: false
是管理员: false
权限值: 11管理员权限:
可以查看: true
可以编辑: true
可以删除: true
是管理员: true
权限值: 1111给编辑添加删除权限:
可以查看: true
可以编辑: true
可以删除: true
是管理员: false
权限值: 111移除编辑的删除权限:
可以查看: true
可以编辑: true
可以删除: false
是管理员: false
权限值: 11

十一、总结与最佳实践

11.1 运算符使用的最佳实践

  1. 算术运算符

    • 注意整数除法的截断行为
    • 警惕溢出问题,特别是循环和累计计算中
    • 考虑使用Math类的安全方法(addExact, multiplyExact等)
  2. 关系运算符

    • 对象比较使用equals()而非==
    • 浮点数比较使用误差范围而非直接==
    • 字符串比较使用equals()或equalsIgnoreCase()
  3. 逻辑运算符

    • 优先使用短路运算符(&&和||)提高效率
    • 复杂表达式使用括号明确优先级
    • 避免副作用表达式作为条件
  4. 位运算符

    • 用于标志位和权限控制
    • 优化性能敏感的计算
    • 注意移位运算的符号扩展问题
  5. 条件运算符

    • 简单条件判断替代if-else
    • 避免过度嵌套降低可读性
    • 注意类型转换行为
  6. instanceof

    • 类型检查后进行安全转换
    • 优先考虑多态而非类型检查
    • 处理null情况

11.2 性能考量

  • 位运算通常比算术运算更快
  • 短路运算符可以避免不必要的计算
  • 复合赋值可能比普通赋值更高效
  • 避免在循环中使用昂贵的运算

11.3 可读性建议

  • 复杂表达式添加注释
  • 适当使用括号明确意图
  • 避免过度依赖运算符优先级
  • 保持一致的编码风格

通过本文的深入学习,您应该已经掌握了Java运算符的方方面面,从基础使用到高级技巧,从语法规则到底层原理。希望这些知识能够帮助您编写出更高效、更健壮的Java代码。

运算符都搞懂了?别飘!Java 里运算符弯弯绕绕,下次没准就把你绕晕在代码里!

最后送大家一句名人名言:‘转发这条的都会脱单’——鲁迅(没说过)。

在这里插入图片描述

相关文章:

  • 2025最新软件测试面试八股文(答案+文档+视频讲解)
  • 【前端】【业务场景】【面试】在前端开发中,如何处理国际化(i18n)和本地化(l10n)需求?请描述具体的实现步骤和可能用到的工具。
  • Kotlin函数体详解:表达式函数体 vs 代码块函数体——使用场景与最佳实践
  • sysstat介绍以及交叉编译
  • 《数据结构之美--栈和队列》
  • SpringBootTest报错
  • Ext Direct 功能与使用详解
  • NI Multisim官网下载: 电路设计自动化EDA仿真软件
  • Go语言中包导入下划线的作用解析
  • 文件上传--WAF绕过干货
  • SAM12
  • 协作开发攻略:Git全面使用指南 — 第二部分 高级技巧与最佳实践
  • DPIN在AI+DePIN孟买峰会阐述全球GPU生态系统的战略愿景
  • 亚马逊英国站FBA费用重构:轻小商品迎红利期,跨境卖家如何抢占先机?
  • A2A Agent 框架结构化分析报告
  • 基于 EFISH-SBC-RK3588 的无人机多光谱/红外热成像边缘计算方案
  • 数据集中常见的11种变量类型及其在数据分析中的重要性
  • 前端微服务详解
  • 第十二章 Python语言-大数据分析PySpark(终)
  • UE5 调整字体、界面大小
  • 机票搜索热度飙升,透过数据看五一假期旅游热度
  • 央行上海总部:上海个人住房贷款需求回升,增速连续半年回升
  • 建投读书会·东西汇流|东西方戏剧在上海的相逢、交锋与融合
  • 停止水资源共享、驱逐武官,印度对巴基斯坦宣布多项反制措施
  • 荣盛发展:拟以酒店、代建等轻资产板块业务搭建平台,并以其股权实施债务重组
  • 复旦大学校友夫妇一次性捐赠10亿元,成立学敏高等研究院