Mega Code Archive

 
Categories / Java / Language Basics
 

Demonstrates short-circuiting behavior with logical operators

//: c03:ShortCircuit.java // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 // www.BruceEckel.com. See copyright notice in CopyRight.txt. public class ShortCircuit {   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");   } } ///:~