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.
C++
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cin >> num1 >> num2;
cout << num1 + num2;
return 0;
}
Input:
54
4
Output:
58
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.
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
System.out.println("GeeksForGeeks");
}
}
Output:
GeeksforGeeks
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.NO | C++ Return 0 | Java 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. |
Similar Reads
return Statement in C++ In C++, the return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was calle
4 min read
exit() vs _Exit() in C/C++ exit() and _Exit() in C/C++ are very similar in functionality. However, there is one difference between exit() and _Exit() and it is that exit() function performs some cleaning before the termination of the program like connection termination, buffer flushes, etc. exit() In C, exit() terminates the
3 min read
What does main() return in C and C++? C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our t
3 min read
exit(0) vs exit(1) in C/C++ with Examples exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status. There are two types of exit status in C/C++: Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been
2 min read
std::endl vs
in C++ std::endl and \n both seem to do the same thing but there is a subtle difference between them. std::cout << std::endl inserts a new line and flushes the stream(output buffer), whereas std::cout << "\n" just inserts a new line. Therefore, std::cout << std::endl; can be said equivale
3 min read