[Java] Difference between Logical Operators &, &&, |, ||

Logical operators include

  • Logical OR |, ||
  • Logical AND &, &&
  • Logical eXclusive OR ^

But why Logical OR and Logical AND has two different operators? What difference between | and ||? What difference between & and &&?

如果你想閱讀中文版本,請點 這裡

| and || both are Logical OR, but the difference between these two is once the left side operation of || could decide the result, it would not operate the left side operation of ||, but | will compare expressions after the expression executed.

For example,

class LogicalOperatorOR {
    public static void main (String[] args) throws java.lang.Exception {
        int i = 3, j = 4, k = 5;
    System.out.println("Result with |:" + ((j < k++) | (i++ == j))); // i++ would operate
    System.out.println("i after operation:" + i);
    System.out.println(" k after operation:" + k);

    i = 3;
    j = 4;
    k = 5;
    System.out.println("Result with ||:" + ((j < k++) || (i++ == j))); // i++ would not operate
    System.out.println("i after operation:" + i);
    System.out.println(" k after operation:" + k);
    }
}

Output

Result with |:true
i after operation:4
j after operation:6
Result with ||:true
i after operation:3
j after operation:6

In the 11th line, because the expression on the left of the || operator is true, it needn't look at the expression on the right to know that the result is true, so i++ == j will not be executed, i will not increase.

It's the same in "Logical AND". When the expression on the left of && can determine the result of the operation, the expression on the right will not be executed.

For example,

class LogicalOperatorAND {
    public static void main (String[] args) throws java.lang.Exception {
        int i = 3, j = 4, k = 5, l = 6;
    System.out.println("Result with &:" + ((i++ == j++) & (k++ == l))); // k++ would operate
    System.out.println("i after operation:" + i);
    System.out.println("j after operation:" + j);
    System.out.println("k after operation:" + k);

    i = 3;
    j = 4;
    k = 5;
    l = 6;
    System.out.println("Result with &&:" + ((i++ == j++) && (k++ == l))); // k++ would not operate
    System.out.println("i after operation:" + i);
    System.out.println("j after operation:" + j);
    System.out.println("k after operation:" + k);
    }
}

Output

Result with &:false
i after operation:4
j after operation:5
k after operation:6
Result with &&:false
i after operation:4
j after operation:5
k after operation:5

In the 13th line, the expression on the left of the && operator is false, so you needn't look at the expression on the right to know that the result is false, so k++ == l will not be executed and k will not increase.

|| and && are also called conditional operators, since their short-circuit behaviors, they are also called short-circuit OR and short-circuit AND.

The above are some exercises on AND and OR. If you have any suggestions, please leave a message below. Thank you.

No Comments Yet