[Logical Operators] The difference between && and & in Java || and |

Both && and & represent the AND operator, and the result is false if it is false. && is the short-circuit AND operator. If the value of expression A is false, the expression B will not be executed; & represents the logical AND operator, and the expression A and Both
|| and | represent the OR operator, and the result is true if it is true, which is a short-circuit OR operator. If the value of expression A is true, the expression B will not be executed; | represents the logical OR operator , both expression A and expression B will execute

One, the difference between && and &

  1. Operation rules:

    && is the short circuit AND operator. If the first condition is false, then the second condition will not be executed. The second condition is evaluated only if the first condition is true.

    & is the logical AND operator. Regardless of whether the result of the first condition is true or false, the second condition will continue to be evaluated.

  2. effectiveness:

    Since && is a short-circuit AND operator, when the first condition is false, the judgment of the second condition will be skipped directly, so the execution efficiency of the program can be improved.

    & is a logical AND operator, which will continue to evaluate the second condition regardless of the result of the first condition, so it may increase the execution time of the program.

  3. scenes to be used:

    && is typically used when two conditions are to be evaluated and the second condition depends on the result of the first condition.

    When both conditions need to be judged and there is no dependency between the two conditions, & can be used.

  4. Code example:

    int a = 5;
    int b = 10;
    // 使用&&运算符
    if (a > 0 && b > 0) {
          
          
        System.out.println("a和b都大于0");
    }
    // 使用&运算符
    if (a > 0 & b > 0) {
          
          
        System.out.println("a和b都大于0");
    }
    

    In the above example, when both a and b are greater than 0, the results of the two writing methods are the same. But when a is not greater than 0, the conditional judgment using the && operator will directly return false, and the second conditional judgment will not be executed; while the conditional judgment using the & operator will continue to execute the second conditional judgment.

Second, the difference between || and |

  1. Operation rules:

    || is the short-circuit or operator. If the first condition is true, then the second condition will not be executed. The second condition is evaluated only if the first condition is false.

    | is the logical OR operator. Regardless of whether the result of the first condition is true or false, the second condition will continue to be evaluated.

  2. effectiveness:

    Since || is a short-circuit or operator, when the first condition is true, the judgment of the second condition will be skipped directly, so the execution efficiency of the program can be improved.

    | is a logical OR operator, regardless of the result of the first condition, it will continue to evaluate the second condition, so it may increase the execution time of the program.

  3. scenes to be used:

    When only one of the two conditions needs to be judged to be true, || is usually used.

    When both conditions need to be judged and there is no dependency between the two conditions, | can be used.

  4. Code example:

    int a = 5;
    int b = 10;
    // 使用||运算符
    if (a > 0 || b > 0) {
          
          
        System.out.println("a和b至少有一个大于0");
    }
    // 使用|运算符
    if (a > 0 | b > 0) {
          
          
        System.out.println("a和b至少有一个大于0");
    }
    

    In the above example, when at least one of a and b is greater than 0, the results of the two writing methods are the same. But when a is not greater than 0, the conditional judgment using the || operator will continue to execute the judgment of the second condition; and the conditional judgment using the | operator will continue to execute the judgment of the second condition.

Guess you like

Origin blog.csdn.net/gjwgjw1111/article/details/132601832