Java - Logical Short-Circuit AND Operator &&

What is Logical Short-Circuit AND Operator?

The logical short-circuit AND operator (&&) is used in the form

operand1 && operand2 
  

The operator returns true if both operands are true. If either operand is false, it returns false.

If operand1 evaluates to false, it returns false without evaluating operand2. This is why it is called a short-circuit AND operator.

Example

The following code shows how to Logical Short-Circuit AND Operator

int i = 10; 
int j = 15; 
boolean b = (i > 5 && j > 10);  // Assigns true to b 

Demo

public class Main {
  public static void main(String[] args) {
    int i = 10; /*from  w  w w  .ja  v  a  2 s  . co  m*/
    int j = 15; 
    boolean b = (i > 5 && j > 10);  // Assigns true to b 

    System.out.println(b);
  }
}

Result

Quiz