Java System.exit(0) 的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/457338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of System.exit(0)
提问by Warrior
public class WrapperTest {
static {
print(10);
}
static void print(int x) {
System.out.println(x);
System.exit(0);
}
}
In the above code System.exit(0)
is used to stop the program. What argument does that method take? Why do we gave it as 0
. Can anyone explain the concept?
在上面的代码中System.exit(0)
是用来停止程序的。该方法采用什么参数?为什么我们把它作为0
. 谁能解释一下这个概念?
采纳答案by Xn0vv3r
From the JAVA Documentation:
从JAVA 文档:
The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
参数用作状态代码;按照惯例,非零状态代码表示异常终止。
And Wikipediaadds additional information.
和维基百科增加了额外的信息。
回答by Joachim Sauer
It's the return value that the Java process will report to the calling process.
它是 Java 进程将报告给调用进程的返回值。
It hasn't really got a precise definition, but the usual convention is that 0
means success and any non-zero value represents a failure.
它并没有真正的精确定义,但通常的约定是这0
意味着成功,任何非零值都表示失败。
回答by Michael Borgwardt
The argument is the return code which the java process will return (0 means "successful"). It can be used when a Java program is a part of a batch script, or by build tools such as Ant.
参数是 java 进程将返回的返回码(0 表示“成功”)。当 Java 程序是批处理脚本的一部分时,或由构建工具(如 Ant)使用时,可以使用它。