Using a boolean value to control the if statement

The value of a boolean variable is sufficient, by itself, to control the if statement.


public class Main {
  public static void main(String args[]) {
    boolean b;
    b = false;
    if (b) {
      System.out.println("This is executed.");
    } else {
      System.out.println("This is NOT executed.");
    }

  }
}

There is no need to write an if statement like this:


if(b == true) ...

The output generated by this program is shown here:


This is NOT executed.
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.