一、方法定义与功能说明

public static boolean logicalAnd(boolean a, boolean b)

功能:执行逻辑与(AND)操作,等效于 a && b
引入版本:Java 8 (JDK 1.8)
设计目的:为函数式编程提供方法引用支持

真值表: | 输入 a | 输入 b | 返回值 | |--------|--------|--------| | true | true | true | | true | false | false | | false | true | false | | false | false | false |

二、核心特性解析

  1. 非短路求值
    && 操作符不同,logicalAnd 不会短路求值,始终计算两个参数:

    // 使用 && 操作符(短路行为)
    boolean shortCircuit = (1 > 2) && (10 / 0 > 0);  // 不抛出异常(跳过第二表达式)
    
    // 使用 logicalAnd(非短路)
    boolean noShortCircuit = Boolean.logicalAnd(1 > 2, 10 / 0 > 0); // 抛出ArithmeticException
    
  2. 函数式接口兼容
    可作为 BinaryOperator<Boolean>BooleanBinaryOperator 使用:

    BinaryOperator<Boolean> andOperator = Boolean::logicalAnd;
    

三、示例代码

基础用法

System.out.println(Boolean.logicalAnd(true, true));   // true
System.out.println(Boolean.logicalAnd(true, false));  // false
System.out.println(Boolean.logicalAnd(false, true));  // false
System.out.println(Boolean.logicalAnd(false, false)); // false

函数式编程应用

// 场景1:结合Stream检查所有元素满足条件
List<Integer> numbers = Arrays.asList(2, 4, 6, 8);
boolean allEven = numbers.stream()
                         .map(n -> n % 2 == 0)
                         .reduce(true, Boolean::logicalAnd);
System.out.println(allEven); // true

// 场景2:自定义组合条件验证器
public static Validator andValidator(Validator first, Validator second) {
    return input -> Boolean.logicalAnd(first.validate(input), second.validate(input));
}

// 使用示例
Validator isPositive = n -> n > 0;
Validator isEven = n -> n % 2 == 0;
Validator combined = andValidator(isPositive, isEven);

System.out.println(combined.validate(4)); // true
System.out.println(combined.validate(-2)); // false

四、使用技巧与最佳实践

  1. 函数式场景优先
    在需要方法引用的地方替代 &&

    // 传统方式
    if (condition1() && condition2()) { ... }
    
    // 函数式重构
    BinaryOperator<Boolean> and = Boolean::logicalAnd;
    if (and.apply(condition1(), condition2())) { ... }
    
  2. 组合复杂逻辑
    构建可复用的逻辑组合器:

    public static boolean multiAnd(boolean... conditions) {
        return Arrays.stream(conditions)
                     .reduce(true, Boolean::logicalAnd);
    }
    
  3. 明确非短路语义
    当需要强制求值两个表达式时使用:

    boolean hasSideEffect = Boolean.logicalAnd(updateDatabase(), sendNotification());
    

五、常见错误与注意事项

  1. 非短路陷阱
    包含副作用的表达式可能引发意外行为:

    // 危险:即使第一个条件为false,仍会执行deleteFile()
    Boolean.logicalAnd(isSafeMode(), deleteFile());
    
    // 安全方案:显式短路控制
    if (isSafeMode()) {
        deleteFile();
    }
    
  2. 空指针风险
    参数为包装类型时需处理null:

    Boolean flagA = null;
    Boolean flagB = true;
    
    // 错误:抛出NullPointerException
    Boolean.logicalAnd(flagA, flagB);
    
    // 正确:空安全处理
    boolean safeResult = (flagA != null && flagB != null) 
                         ? Boolean.logicalAnd(flagA, flagB)
                         : false;
    
  3. 性能误用
    在循环中避免低效使用:

    // 低效:每次迭代创建函数对象
    for (int i = 0; i < 1000; i++) {
        result = Boolean.logicalAnd(result, check(i));
    }
    
    // 高效:直接使用 &&
    for (int i = 0; i < 1000; i++) {
        result = result && check(i); // 且有短路优化
    }
    

六、与相关方法对比

方法 类型 短路求值 函数式支持 适用场景
a && b 操作符 常规条件判断
Boolean.logicalAnd() 静态方法 Stream/函数式编程
& (按位与) 操作符 整数位操作/布尔非短路

七、总结与最佳实践

  1. 核心价值

    • 为Java函数式编程提供逻辑操作的方法引用
    • 实现明确的非短路求值语义
    • 增强条件组合的代码表达能力
  2. 使用决策树

    graph TD
      A[需要逻辑与操作] --> B{是否函数式场景?}
      B -->|是| C[使用 Boolean.logicalAnd]
      B -->|否| D{需要短路求值?}
      D -->|是| E[使用 && 操作符]
      D -->|否| F[使用 & 操作符]