Involve calculation in the short-circuit logical operators : boolean Operators « Operators « SCJP






public class MainClass {
  public static void main(String[] argv) {
    int val = 0;
    boolean test = (val == 0) || (++val == 2);
    System.out.println("test = " + test + "\nval = " + val);

  }
}
test = true
val = 0
public class MainClass {
  public static void main(String[] argv) {
    int val = (int) (2 * Math.random());
    boolean test = (val == 0) | (++val == 2);
    System.out.println("test = " + test + "\nval = " + val);
  }
}
test = true
val = 1








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