Java Exit

Java Exit


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.

public static void exit(int)
Following program gives the usage of exit() method.

Example on Java Exit


Java Exit
Output screenshot on Java Exit

if(i==6)
{
System.exit(0);
}

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.

1. Java Nested Classes
2. Static Nested Classes
3. Nested Interfaces
4. Interface inside Interface
5. Nested Interface within Class
6. Class inside Interface

Leave a Reply

Your email address will not be published. Required fields are marked *