Demonstrates short-circuiting behavior : Logical Operators « Operators « Java Tutorial






public class MainClass {

  static boolean test1(int val) {
    System.out.println("test1(" + val + ")");
    System.out.println("result: " + (val < 1));
    return val < 1;
  }

  static boolean test2(int val) {
    System.out.println("test2(" + val + ")");
    System.out.println("result: " + (val < 2));
    return val < 2;
  }

  static boolean test3(int val) {
    System.out.println("test3(" + val + ")");
    System.out.println("result: " + (val < 3));
    return val < 3;
  }

  public static void main(String[] args) {
    if (test1(0) && test2(2) && test3(2))
      System.out.println("expression is true");
    else
      System.out.println("expression is false");
  }
}
test1(0)
result: true
test2(2)
result: false
expression is false








3.7.Logical Operators
3.7.1.Boolean Logical Operators
3.7.2.The following table shows the effect of each logical operation
3.7.3.Logical operators in action
3.7.4.Demonstrate the boolean logical operators
3.7.5.Logical Operators
3.7.6.AND operator
3.7.7.&& versus &
3.7.8.Logical OR Operations
3.7.9.Boolean NOT Operations: applies to one boolean operand
3.7.10.Demonstrates short-circuiting behavior