Short-Circuit Logical Operators : boolean Operators « Operators « SCJP






The short-circuit logical operators && and || 

&& short-circuit AND
|| short-circuit OR

&& and || are similar to the & and | operators.
&& and || are applicable only to boolean values and not integral types. 
&& and || have the ability to "short circuit" a calculation if the result is definitely known. 
&& and || are useful for null-reference-handling in Java programming. 
The right operand might not be evaluated in the && and || 


  public class MainClass {
    public static void main(String[] args) {
      if ((isItSmall(3)) || (isItSmall(7))) {
        System.out.println("Result is true");
      }
      if ((isItSmall(6)) || (isItSmall(9))) {
        System.out.println("Result is true");
      }
   }

   public static boolean isItSmall(int i) {
     if (i < 5) {
       System.out.println("i < 5");
       return true;
     } else {
       System.out.println("i >= 5");
       return false;
     }
   }
 }
i < 5
Result is true
i >= 5
i >= 5








2.8.boolean Operators
2.8.1.Java Boolean operators
2.8.2.Short-Circuit Logical Operators
2.8.3.Using the Boolean and Logical Short-Circuit Operators
2.8.4.Logical Operators (Not Short-Circuit)
2.8.5.Java does not permit you to cast any type to boolean
2.8.6.Involve calculation in the short-circuit logical operators