How to end program in Java?

Total
0
Shares

In order to know how to end program in Java, you should be aware that calling return with in a function doesn’t end it. You need to use System.exit(0) command to terminate the Java Virtual Machine.

public void endProgram(){
  System.out.println('This will print');
  System.exit(0);
  System.out.println('This can not print');
}

    Tweet this to help others

In this code the first command will execute and print the string. But, third print statement will not execute. System.exit(0) will end the program and terminate the JVM before the java code reach the 3rd line.