How to end a program in an if statement in Java?

To end a Java program in an if statement, call the System.exit method.

To end a Java program if the statement is true, do this:

boolean statement = true;

if (statement) {
  System.exit(0);
} else {
  // do something else
}

To end a Java program if the statement is false, do this:

boolean statement = true;

if (statement) {
  // do something else
} else {
  System.exit(0);
}