How to end program in Java?


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.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.

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like

[Code Example] K Lists In Java

Table of Contents Hide IntroductionLet’s Dive In;klist CommandsCode ExampleResultsRelated Posts: Introduction K-list method displays the entries in the local credentials cache and key table in the command line of the…
View Post

Java SimpleDateFormat – SSSXXX

The Java SimpleDateFormat class can represent many date formats including SSSXXX. Each letter in the date can signify date, time, timezone or some other things. SSSXXX represents milliseconds and timezone.…
View Post

How to compare chars in Java?

Table of Contents Hide Compare chars, if defined using char datatypeIf defined using Character classMerged Code ExampleLive DemoRelated Posts: If you are wondering how to compare chars in java, then…
View Post

How to clear Java cache?

Table of Contents Hide Open Java control panelClear Java CacheRelated Posts: You should know how to clear java cache because temporary java files can impact the browser’s performance as well…
View Post