Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Thursday, November 18, 2021

Java Exit Program - How to end program in java?

1. Overview

In this tutorial, We'll learn how many ways to end java program and how to terminate program execution in java.

Stopping the java program can be done in two ways as below.

A) Using System.exit() method
B) Using return statement
How to end program in java?


2. End and Terminate program using System.exit() 


Use System.exit() method to end the java program execution. exit() method is from the System class and exit() is a static method.

System.exit() method gives the instructions to JVM to terminate the current running program from the point of exit() method.

Look at the below example program.
package com.javaprogramto.programs.exit;

public class JavaProgramExitExample {

	public static void main(String[] args) {
		
		System.out.println("hello java developers ");
		System.out.println("understand how System.exit() method works");
		
		System.exit(0);
		
		System.out.println("Last line from this program");
	}
}

Output:
hello java developers 
understand how System.exit() method works

Process finished with exit code 0

From the above code, we expect the 3 statement to be printed on the console from three System.out.println() statements But it has prinited only first two.

Because System.exit() is after the second sysout. Wheneven java runtime finds exit() method, it just stops the program execution at any point of time.

If you observe exit() method that takes an integer value and we passed zero in the abvoe example.
0 value indicates to jvm exit the program with successful finishing with no errors. That is graceful termination.

but if you pass any non zero value leads to print with the error message. But from java 8 or above versions, it does not show any error message even if non zero value is pased.

Non zero system.exit() samples
System.exit(2);
System.exit(-1);
System.exit(3);

3. Java end program using return statement


We can use return statements from the methods or from the main program to end the current method execution and continue from its parent.

This is very useful to end the function execution rather than terminating the JVM itself.

return is a keyword from java api and optionally return the values.

Look at the below code.

package com.javaprogramto.programs.exit;

public class JavaProgramExitExample2 {

	public static void main(String[] args) {
		
		System.out.println("satement 1");
		System.out.println("statement before return statement");
		
		return;
		
		System.out.println("Last line from this program");
	}
}

This program does not compile because there is a sysout statement after return statement. That mean compiler detects that last line of the program is never going to be executed with the error message "Unreachable code".
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Unreachable code

	at com.javaprogramto.programs.exit.JavaProgramExitExample2.main(JavaProgramExitExample2.java:12)

To fix this error, we need to remove the statements after return.

Return from a method:
package com.javaprogramto.programs.exit;

public class JavaProgramExitExample3 {

	public static void main(String[] args) {

		System.out.println("first line from main method");

		method();

		System.out.println("last line from main method");
	}

	private static void method() {
		
		System.out.println("executing the method");
		return;
	}
}

Output:
first line from main method
executing the method
last line from main method

4. Java end program using return statement with if else


We can also control the end of the method using an if else statement based on the conditions.

If the condition is met then execute the logic and return "even number" and from else condition return "odd number" value.

package com.javaprogramto.programs.exit;

public class JavaProgramExitExample4 {

	public static void main(String[] args) {

		System.out.println("first line from main method");

		int number = 10;
		String value = getValue(number);
		System.out.println(value);
		System.out.println("last line from main method");
	}

	private static String getValue(int number) {
		
		System.out.println("statement 1");
		
		if(number % 2 == 0) {
			return "even number";
		}
		else {
			return "odd number";
		}
	}

}

Output:
first line from main method
statement 1
even number
last line from main method

5. Conclusion


In this article, we've seen how to end the program in java using System.exit() and return statement.

In the real time, system.exit() is used in the standalone and concurrent applications on the particular conditions met. But, prefer to throw the custom exception with the correct error message for better debugging.

return; does nothing returned and similar to the void.




No comments:

Post a Comment

Please do not add any spam links in the comments section.