Skip to content
Related Articles

Related Articles

Improve Article

Java System.exit(0) vs C++ return 0

  • Last Updated : 01 Sep, 2020

Java and C++ are languages with different applications and design goals. C++ is an extension of procedural programming language C and Java relies on a Java virtual machine to be secure and highly portable. This leads them to many differences. In this article, we will see the difference between C++ return 0 and Java System.exit(0). Before getting into the differences, let us first understand what each of them actually means.

C++ return 0

  • In Standard C++, it is recommended to create a main() function with a return type.
  • So, the main() function must return an integer value and this integer value is usually the value that will be passed back to the operating system.

In stdlib.h the macros EXIT_SUCCESS and EXIT_FAILURE are defined like this :

#define EXIT_SUCCESS    0
#define EXIT_FAILURE    1
  • return 0 –> successful termination.
  • return 1 or any other non-zero value –> unsuccessful termination.
  • Returning different values like return 1 or return -1 or any other non-zero value means that program is returning error.


#include <iostream>
using namespace std;
int main()
    int num1, num2;
    cin >> num1 >> num2;
    cout << num1 + num2;
    return 0;


Java System.exit(0)

The first thing to take into consideration is that the main function in java has a return type void.

  • In Java, you can’t return the exit code, because it’s a void function. So, if you want to explicitly specify an exit code, you have to use System.exit() method.
  • The java.lang.System.exit() method exits the current program by terminating running Java virtual machine.

Declaration for java.lang.System.exit() method:

public static void exit(int status)
exit(0) -->successful termination.
exit(1) or exit(-1) or any other non-zero value –-> unsuccessful termination.


class GFG {
    public static void main (String[] args) {

Note: The work of both return 0 and System.exit(0) is the same as the difference in the return type of the main() functions.  

The following table describes the differences:

SR.NOC++ Return 0Java System.exit(0) 
1.The main() function in C++ has a return type. Hence, every main method in C++ should return any value.The main() method in java is of void return type. Hence, main method should not return any value.
2.In a C++ program return 0 statement is optional: the compiler automatically adds a return 0 in a program implicitly.In Java, there is no special requirement to call System.exit(0) or add it explicitly.
3.It is a keyword which is used to return some value, so it does not need any declaration.It needs only return keyword with value or variables.   

Declaration for java.lang.System.exit() method:

public static void exit(int status)

4.Generally, return 0 is used to return exit code to the operating system.If we want to explicitly specify an exit code to the operating system, we have to use System.exit().
5.Using return 0 in C++ programs is considered a good practice.Using System.exit(0) is avoided in practice because we have our main() method with void return type.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :