Not understanding System.exit(0).
In Craig's Intro to Java class he sets up a conditional statement for age restriction, but within that conditional statement he also includes system.exit with a value of 0. I'm not understanding:
1) Why do we need to have a special exit program code within a conditional? Wouldn't the conditional either be met or not and exit on its own?
2) When do we use this system.exit(0)?
3) Why do we use system.exit(0) at all? It seems similar to ejecting a flash drive in terms of a safer(?) way to exit a program than just abruptly exiting...but I'm not clear on that...
3A) If it is "safer" to use the system.exit(0) in some situations, how do we determine
the "safety" level of a particular part of a program?
4) Aren't we forcing the program to an artificial safe exit using system.exit(0) vs. letting it exit naturally?
5) What does 0 stand for or mean? Craig says it means that we exited normally, but in what... the compiler?
System.exit() ends the program. The code directly following the "System.exit()" call does not execute. Yes, If Statements terminate after the closing bracket, but what Craig wanted to do was end the program.
You use it to terminate the JVM.
You seem to think that the program would have ended just the same if the "System.exit()" call was not made. Based on the video, there was code after the if statement.
The "0" parameter is default. You don't need it (I think). Anything other than "0" indicates that the execution did not go well. You see the numbers in the console. It's meant for error-finding.
Marc Busby24,016 Points
As Billy said, the System.exit() stops the program running, without it the 'if' statement does return a value but the program will still run the rest of the code.