Classes/Objects  «Prev  Next»


Lesson 3 Logical operators
ObjectiveDescribe important points about the logical operators.

Java Logical operators

One of the exam objectives requires you to be able to analyze expressions involving the operators &, |, &&, and ||, and identify which operands are evaluated and the resulting value of the expression. The reason behind this objective is that the && and || operators exhibit a special " short-circuit" behavior when they are evaluated as part of a logical expression. When an expression x && y is evaluated, the first operand x is evaluated. If x is false, then the evaluation of the second operand is skipped and the value of false is returned as the value of the expression. When an expression x || y is evaluated, if x is true then the evaluation of the second operand is skipped and the value of true is returned as the value of the expression. To see an example of the &, |, &&, and || operators in action, compile and run this ShortCircuit program. (See below)
The ternary operator also exhibits short-circuit behavior. If the first operand evaluates to true, the evaluation of the third operand is skipped. Similarly, if the first operand is false, the evaluation of the second operand is skipped. The Ternary Operator Behavior provides an example of the short-circuit[1] behavior of the ternary operator.

Java Logical AND OR: The ShortCircuit program

The ShortCircuit program illustrates the short-circuit behavior of the && and || operators. It displays the following output:
false true false
false false
true false true
true true

You should note that only two values are displayed on the second and fourth lines. That's because in these cases, the && and || operators skipped the evaluation of the second operands. These operands were evaluated by the & and | operators.
ShortCircuit
public class ShortCircuit {

  public static boolean display(boolean b) {
    System.out.print(b + " ");
    return b;
  }

  public static void main(String[] args) {
    boolean t = true;
    boolean f = false;
    System.out.println(display(f) & display(t));
    System.out.println(display(f) && display(t));
    System.out.println(display(t) | display(f));
    System.out.println(display(t) || display(f));
  }
}


Ternary Operator Behavior : The ShortCircuit2 Program

The ShortCircuit2 program shows how the ternary operator exhibits short-circuit behavior. The results displayed by this program are:
true
false

Note that in both cases the ternary operator skipped the evaluation of either the second or third operands.
ShortCircuit2.java
public class ShortCircuit2 {
  public static boolean display(boolean b) {
    System.out.println(b);
    return b;
  }

  public static void main(String[] args) {
    boolean t = true;
    boolean f = false;
    boolean result = t ? display(t) : display(f);
    result = f ? display(t) : display(f);
  }
}


&& AND || ARE SHORT-CIRCUIT OPERATORS

Another interesting point to note with respect to the logical operators && and || is that they are also called short-circuit operators because of the way they evaluate their operands to determine the result. Let us start with the operator && . The && operator returns true only if both the operands are true. If the first operand to this operator evaluates to false, the result can never be true. Therefore, && does not evaluate the second operand. Similarly, the || operator does not evaluate the second operator if the first operand evaluates to true.
int marks = 8;
int total = 10;
System.out.println(total < marks && ++marks > 5); //line 1
System.out.println(marks);                        //line 2
System.out.println(total == 10 || ++marks > 10);  //line 3
System.out.println(marks);                        //line 4

In the first print statement at line1, because the first condition, total < marks, evaluates to false, the next condition, ++marks > 5, is not even evaluated. As you can see at line2, the output value of marks is still 8 (the value to which it was initialized on line 1)! Similarly, in the next comparison at line 3, because total == 10 evaluates to true, the second condition, ++marks > 10, is not evaluated. Again, this can be verified when the value of marks is printed again line 4, and the output is 8.

Table 4.3 Operator Types and the relevant operators
Operator Types and the relevant operators
Operator Types and the relevant operators

Not all operators can be used with all types of operands. For example, you can determine whether a number is greater than another number, but you cannot determine whether true is greater than false or a number is greater than true. Take note of this as you learn the usage of all the operators on the OCA Java SE 7 Programmer I exam.

Logical Operators - Exercise

Click the Exercise link below to practice analyzing code using operators.
Logical Operators - Exercise

[1]Short-circuit: A boolean operation in which one operand determines the result of the operation.

SEMrush Software