How to end program in java

How to end program in java

Ho In this post, we will see how to end program in java.

You can use System.exit(0) to end program in java.

Using System.exit() to end java program

You can use exit() method of System class to end java program.

System.exit() causes Java virtual machine to exit and terminate the current process. You can call System.exit() anywhere in the program, resulting in JVM termination.

As System.exit() is the static method of System class, the compiler does not know what it will do, hence does not give unreachable code error.

If we pass 0 to System.exit() method, it indicates that termination happened successfully and pass this value to operating system.

  • exit(0): It indicates successful end of java program.
  • exit(-1) or exit(1) or exit(any non zero value): It indicates unsuccessful end of java program.

Example to demonstrate System.exit() method

Here is an example:


Before calling System.exit()

As you can see, it did not print After calling Syste.exit() after calling System.exit() method.

How to end java program in an if statement in java

You can exit program in java based on if statement condition.
Here is an example.


Terminate the JVM

As you can see, program ended as soon as array element was greater or equals to 40 while iterating. It neither continued the loop nor printed Loop ends here after calling System.exit() method.

Using return to end current method

If you just want to exit from current method, you can use return statement.

return statement stops the execution of current method and return it to calling method. It is a reserved keyword which compiler already knows.

return can end java program if it is the last statement inside main method of class being executed. If you put code after a return statement, compiler will give error about unreachable code.

Here is an example:

Preceding java program will give you compilation error about unreachable code at line number 6.
Another example:
Let’s say you want to write isPrime method to check if number is prime or not. You can call isPrime() method from main method and return true or false based on whether number is prime or not.


is 17 Prime: true
is 27 Prime: false
is 79 Prime: true

That’s all about how to end program in java.

Related Posts

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.