Sobes.tech
Junior — Middle

Explain the purpose and operation of the System.exit function in Java.

sobes.tech AI

Answer from AI

The System.exit(int status) function in Java is used to terminate the current JVM process. The status argument is the exit code, which usually 0 indicates successful completion, and any non-zero value indicates an error.

When System.exit is called:

  • all registered shutdown hooks are executed,
  • all threads are terminated,
  • resources are released,
  • the JVM terminates with the specified code.

Example:

public class Main {
    public static void main(String[] args) {
        System.out.println("The program will exit with code 0");
        System.exit(0);
        // Code after System.exit will not be executed
    }
}