Easy steps to end program in java – ( 2022 Updated)

Java is one of the most popular programming languages that any developer or programmer must have knowledge of. As you know, there is a lot of awareness of the Java language. However, how to end a java program is one of the important things you should learn. This article shows the best ways for java how to end a program. Let’s look at some examples to understand the situation and find the easiest way to deal with it.

Using System.exit() to end java program

In the first place, for java how to end a program, you can end it by using the exit() method of the System class. This is a quite simple way to end a program. “Java virtual machines” will exit and terminate the current process, which is caused by System.exit(). You can call System.exit() anywhere in your program. This will terminate the JVM.

Following this is a simple example using the System.exit() method:

Passing 0 to the System.exit() method indicates a successful termination with no error and passes that value to the operating system. The other statuses 1 or -1 ( non-zero status ) instruct the compiler to terminate the program but the unsuccessful end of the java program. In the example below, you can see that only statement 1 is printed in the output. This is because the program exits after execution and statement 2 was not executed.

Example:

public class JavaHelloWorld {

    public static void main(String[] args) {

        System.out.println("Before calling System.exit()");

        System.exit(0);

        System.out.println("After calling System.exit()");

    }

}

Output:

Statement 1

Process finished with exit code 0

Java how to end the program in an if statement in java

Besides using System.exit() to end the java program, we have many ways. One is ending the program in java based on if statement condition.

package org.arpit.java2blog.published;

public class SystemExitExampleIfStatement{

    public static void main(String[] args)
    {
        int arr[] = {10, 20, 30, 40, 50, 60, 70, 80};

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 40) {
                // end java program
                System.out.println("Terminate the JVM");
                System.exit(0); // Terminates JVM
            } else {
                System.out.println(arr[i]);
            }
        }
        System.out.println("Loop ends here");
    }
}

Output:

10

20

30

Terminate the JVM

 

As we can see, the program terminated as soon as there were 40 or more array elements during iteration.  Neither the loop continue nor is printed after the System.exit() method is called, and does not the loop exit appear here.

How to end a program in java using return

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

The return statement stops the execution of the current method and returns to the calling method. This is a reserved keyword already known to the compiler.

In formal languages, “return” can be used to complete method execution. This means that you can exit the function this way.

The example below shows how to return a “string” from a function. If the value is “false”, “statement 2” is returned and execution of this method is complete, which means “statement 3” is ignored.

public class Main {
    public static void main(String[] args) {
      boolean value = false;

      String getValueFromFunc = func(value);
        System.out.println(getValueFromFunc);
  }

      public static String func(boolean value){
        if(value){
        System.out.println("Statement 1");
        }else{
        return "Statement 2";
        }
        return "Statement 3";
    }

 }

Output: 

Statement 2

But what if the value is true? Let’s discover it in the following example.

The output will print both Statement1 and Statement3, which were ignored in the previous example. It happened because the statement block in the “if” condition contained “println” instead of “return”.

public class Main {

    public static void main(String[] args) {

       boolean value = true;
   
       String getValueFromFunc = func(value);

        System.out.println(getValueFromFunc);
    }
    public static String func(boolean value){
        if(value){
            System.out.println("Statement 1");
        }else{
            return "Statement 2";
        }
        return "Statement 3";
    }
}

Output: 

Statement 1

Statement 3

In Conclusion

You can now end a java program yourself based on this article. We believe that this is useful information for you to enhance your knowledge. In addition, if you’d like to learn more Java language, contact us. We are always 24/7 support.  At ONEXT DIGITAL, we provide Web development services with superior quality to clients over the world. We make sure that we can be your reliable partner to create successful projects and help your business get your target rapidly