Using the Boolean and Logical Short-Circuit Operators : boolean Operators « Operators « SCJP






Expression x && y is evaluated in the following manner:

1. Evaluate x.
2. If x is false, return false.
3. Otherwise, if x is true evaluate y.
4. Return the value of y.



The expression x || y is evaluated as follows:

1. Evaluate x.
2. If x is true return true.
3. Otherwise, if x is false evaluate y.
4. Return the value of y.



public class MainClass{
 static boolean secondPart(boolean b) {
  System.out.print("secondPart! ");
  return b;
 }
 public static void main(String args[]) {
  boolean t = true;
  boolean f = false;
  System.out.println(false & secondPart(true));
  System.out.println(false && secondPart(true));
  System.out.println(true | secondPart(true));
  System.out.println(true || secondPart(true));
 }
}

secondPart! false
false
secondPart! true
true








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