Skip to content

5.4 逻辑运算符

逻辑运算符的概念

逻辑运算符用于连接多个条件表达式,返回一个布尔值(true 或 false)。Java 提供了三种逻辑运算符:与(&&)、或(||)和非(!)。

逻辑运算符列表

运算符名称描述示例结果
&&逻辑与当且仅当两个操作数都为 true 时,结果为 truetrue && truetrue
``逻辑或当至少有一个操作数为 true 时,结果为 true
!逻辑非对操作数取反,true 变为 false,false 变为 true!truefalse

逻辑运算符的使用

逻辑与(&&)

逻辑与运算符 && 要求两个操作数都为 true,结果才为 true。

java
public class LogicalAndExample {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = true;
        boolean c = false;
        
        System.out.println("a && b: " + (a && b)); // 输出 true
        System.out.println("a && c: " + (a && c)); // 输出 false
        System.out.println("c && c: " + (c && c)); // 输出 false
        
        // 短路特性:如果第一个操作数为 false,第二个操作数不会被执行
        int x = 10;
        boolean result = (x < 5) && (++x > 5);
        System.out.println("result: " + result); // 输出 false
        System.out.println("x: " + x); // 输出 10,因为第二个操作数没有执行
    }
}

逻辑或(||)

逻辑或运算符 || 要求至少有一个操作数为 true,结果才为 true。

java
public class LogicalOrExample {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        boolean c = false;
        
        System.out.println("a || b: " + (a || b)); // 输出 true
        System.out.println("a || c: " + (a || c)); // 输出 true
        System.out.println("c || c: " + (c || c)); // 输出 false
        
        // 短路特性:如果第一个操作数为 true,第二个操作数不会被执行
        int x = 10;
        boolean result = (x > 5) || (++x > 5);
        System.out.println("result: " + result); // 输出 true
        System.out.println("x: " + x); // 输出 10,因为第二个操作数没有执行
    }
}

逻辑非(!)

逻辑非运算符 ! 用于对操作数取反。

java
public class LogicalNotExample {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        
        System.out.println("!a: " + !a); // 输出 false
        System.out.println("!b: " + !b); // 输出 true
        System.out.println("!!a: " + !!a); // 输出 true(双重否定)
    }
}

逻辑运算符的优先级

逻辑运算符的优先级从高到低依次为:

  1. 逻辑非 !
  2. 逻辑与 &&
  3. 逻辑或 ||

示例

java
public class LogicalPrecedenceExample {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        boolean c = true;
        
        // 逻辑与的优先级高于逻辑或
        boolean result1 = a || b && c; // 先计算 b && c,再计算 a || 结果
        System.out.println("a || b && c: " + result1); // 输出 true
        
        // 使用括号改变优先级
        boolean result2 = (a || b) && c; // 先计算 a || b,再计算结果 && c
        System.out.println("(a || b) && c: " + result2); // 输出 true
        
        // 逻辑非的优先级最高
        boolean result3 = !a || b && c; // 先计算 !a,再计算 b && c,最后计算 ||
        System.out.println("!a || b && c: " + result3); // 输出 false
    }
}

逻辑运算符的应用

1. 多重条件判断

java
int age = 25;
boolean hasLicense = true;

if (age >= 18 && hasLicense) {
    System.out.println("You can drive.");
} else {
    System.out.println("You cannot drive.");
}

2. 范围检查

java
int score = 85;

if (score >= 0 && score <= 100) {
    System.out.println("Score is valid.");
} else {
    System.out.println("Score is invalid.");
}

3. 条件组合

java
String username = "admin";
String password = "123456";

if (username.equals("admin") || username.equals("root")) {
    if (password.equals("123456")) {
        System.out.println("Login successful.");
    } else {
        System.out.println("Incorrect password.");
    }
} else {
    System.out.println("Invalid username.");
}

示例:逻辑运算符的使用

java
public class LogicalApplication {
    public static void main(String[] args) {
        // 登录验证
        String username = "user";
        String password = "password";
        boolean isAdmin = false;
        
        if ((username.equals("user") && password.equals("password")) || (username.equals("admin") && password.equals("admin123"))) {
            System.out.println("Login successful.");
            if (username.equals("admin")) {
                isAdmin = true;
                System.out.println("Welcome, admin!");
            } else {
                System.out.println("Welcome, user!");
            }
        } else {
            System.out.println("Login failed.");
        }
        
        // 资格检查
        int age = 20;
        boolean hasDegree = true;
        boolean hasExperience = false;
        
        if (age >= 18 && (hasDegree || hasExperience)) {
            System.out.println("You are eligible for the job.");
        } else {
            System.out.println("You are not eligible for the job.");
        }
        
        // 逻辑非的使用
        boolean isLoggedIn = false;
        if (!isLoggedIn) {
            System.out.println("Please log in first.");
        }
    }
}

常见问题

1. 短路特性的误用

症状:依赖于第二个操作数的副作用,但由于短路特性,第二个操作数没有执行

解决方案:如果需要执行第二个操作数,不要使用短路逻辑运算符,或者将第二个操作数放在单独的语句中

2. 逻辑运算符与位运算符的混淆

症状:使用 & 而不是 &&,或者使用 | 而不是 ||

解决方案:记住 &&|| 是逻辑运算符,&| 是位运算符

3. 优先级问题

症状:表达式的计算顺序不符合预期

解决方案:使用括号来明确表达式的计算顺序

总结

逻辑运算符是 Java 中用于连接多个条件表达式的工具,包括:

  • 逻辑与 &&:当且仅当两个操作数都为 true 时,结果为 true
  • 逻辑或 ||:当至少有一个操作数为 true 时,结果为 true
  • 逻辑非 !:对操作数取反

逻辑运算符具有短路特性,即如果第一个操作数已经能够确定结果,第二个操作数不会被执行。在使用逻辑运算符时,需要注意优先级和短路特性的影响。

© 2026 编程马·菜鸟教程 版权所有