&与&&的比较

    科技2022-08-17  103

    按位与 &: ** 即使前面的表达式能决定整体表达式的值,但 & 依然计算了后面表达式。

    int math=34; int chinese=54; System.out.println((math >= 60) & (++chinese >= 60)); System.out.println(chinese); 运行结果: false 55

    逻辑与 &&: ** 前面有一个表达式能判断整体结果,就不需要判断后面的表达式。

    int math=34; int chinese=54; System.out.println((math >= 60) && (++chinese >= 60)); System.out.println(chinese); 运行结果: false 54

    短路求值: && 和 || 都遵守短路求值的规则

    则:

    对于 &&,如果左侧表达式值为 false,则表达式的整体的值一定是 false,无需计算右侧表达式。对于 ||,如果左侧表达式值为 true,则表达式的整体的值一定是 true,无需计算右侧表达式。
    Processed: 0.008, SQL: 9