Java Exit: In coding, sometimes, Java programmer may need to terminate the running program (application) if the required condition is not met. Or, at the will of the user (when user wants), the program should get terminated; like when the user clicks a button, the game program should end (terminate) and come out of the program.
Here comes exit(int) method defined as static method in System class. Following is the method signature as defined java.lang.System class.
Example on Java Exit
public class Demo
public static void main(String args)
for(int i = 0; i < 10; i++)
System.exit(0); // Java Exit
System.out.println(i); // prints 0 to 5
Output screenshot on Java Exit
The exit() method takes an integer parameter. The 0 parameter indicates a normal shutdown. Other than 0, any number is given, still the program terminates. But the most accepted way is giving 0.
In general coding, to come out of a for loop is break keyword. With break, you will come out of the loop, but not from the program.
To use exit(0) from GUI application is given in Java Frame Closing – WindowListener.
================Extra Reading on Inner classes=================
Because inner classes usage is growing slowly, they are discussed more elaborately here. Android (Java based Mobile OS developed by Google) uses inner classes very extensively, infact anonymous inner classes are passed as parameters to methods.
In this series following combinations are discussed.