Java - Logical AND Operator &

What is Logical AND Operator?

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

operand1 & operand2 
  

The logical AND operator returns true if both operands are true.

If either operand is false, it returns false.

The logical AND operator & evaluates its right-hand operand even if its left-hand operand evaluates to false.

Example

The following code shows how to Logical AND Operator.

int i = 1; 
int j = 1; 
boolean b; 
  
b = (i > 5 & j > 10);            
b = (i > 2 & ((j = 2) > 15));  
System.out.println ("b = " + b); 
System.out.println ("i = " + i); 
System.out.println ("j = " + j); 

Demo

public class Main {
  public static void main(String[] args) {
    int i = 1; //from  ww  w  .  j  a va  2s .  c  o  m
    int j = 1; 
    boolean b; 
      
    b = (i > 5 & j > 10);            
    b = (i > 2 & ((j = 2) > 15));  
    System.out.println ("b = " + b); 
    System.out.println ("i = " + i); 
    System.out.println ("j = " + j); 

  }
}

Result

Exercise