终止 Java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22452930/
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
Terminating a Java Program
提问by Cataclysm
I found out ways to terminate (shut-down or stop) my Java programs. I found two solutions for it.
我找到了终止(关闭或停止)我的 Java 程序的方法。我找到了两个解决方案。
using return;
When I want to quit or terminate my program execution , I add this.using System.exit();
Sometimes I used it. I read about Sytem.exit()from This Question.
使用回报;
当我想退出或终止我的程序执行时,我添加了这个。使用System.exit();
有时我使用它。我从这个问题中读到了Sytem.exit()。
So, I know a little on both them. But I am still confused as to how they actually work. Please check below codes...
所以,我对他们两个都有一些了解。但我仍然对它们的实际工作方式感到困惑。请检查以下代码...
public class Testing {
public static void main(String... str) {
System.out.println(1);
System.exit(0);
System.out.println(2);
return;
}
}
I am sure that 2will not appear. I would like to know is why return;
or other codes can write below the statement of System.exit(0);
and what was real definition for return;
(because it is strange thing for me return
without any variables or values) ?
我相信2不会出现。我想知道是为什么return;
或其他代码可以写在下面的语句System.exit(0);
以及什么是真正的定义return;
(因为return
没有任何变量或值对我来说很奇怪)?
采纳答案by Aleks G
Calling System.exit(0)
(or any other value for that matter) causes the Java virtual machine to exit, terminating the current process. The parameter you pass will be the return value that the java
process will return to the operating system. You can make this call from anywhere in your program - and the result will always be the same - JVM terminates. As this is simply calling a static method in System
class, the compiler does not know what it will do - and hence does not complain about unreachable code.
调用System.exit(0)
(或与此相关的任何其他值)会导致 Java 虚拟机退出,终止当前进程。您传递的参数将是java
进程将返回给操作系统的返回值。您可以从程序中的任何位置进行此调用 - 结果将始终相同 - JVM 终止。由于这只是在System
类中调用静态方法,编译器不知道它将做什么 - 因此不会抱怨无法访问的代码。
return
statement simply aborts execution of the current method. It literally means return the control to the calling method. If the method is declared as void
(as in your example), then you do not need to specify a value, as you'd need to return void
. If the method is declared to return a particular type, then you must specify the value to return - and this value must be of the specified type.
return
语句只是中止当前方法的执行。它的字面意思是将控制权返回给调用方法。如果该方法声明为void
(如您的示例中所示),则您无需指定值,因为您需要 return void
。如果该方法声明为返回特定类型,则您必须指定要返回的值 - 该值必须是指定类型。
return
would cause the program to exit only if it's inside the main
method of the main class being execute. If you try to put code after it, the compiler will complain about unreachable code, for example:
return
只有当它在main
正在执行的主类的方法内时才会导致程序退出。如果您尝试在其后放置代码,编译器会抱怨无法访问的代码,例如:
public static void main(String... str) {
System.out.println(1);
return;
System.out.println(2);
System.exit(0);
}
will not compile with most compiler - producing unreachable code
error pointing to the second System.out.println
call.
不会用大多数编译器编译 - 产生unreachable code
指向第二次System.out.println
调用的错误。
回答by indivisible
Because System.exit()
is just another method to the compiler. It doesn't read ahead and figure out that the whole program will quit at that point (the JVM quits). Your OS or shell can read the integer that is passed back in the System.exit()
method. It is standard for 0
to mean "program quit and everything went OK" and any other value to notify an error occurred. It is up to the developer to document these return values for any users.
因为System.exit()
这只是编译器的另一种方法。它不会提前阅读并确定整个程序将在那时退出(JVM 退出)。您的操作系统或外壳程序可以读取在System.exit()
方法中传回的整数。0
表示“程序退出,一切正常”和任何其他用于通知发生错误的值是标准的。由开发人员为任何用户记录这些返回值。
return
on the other hand is a reserved key word that the compiler knows well.
return
returns a value and ends the current function's run moving back up the stack to the function that invoked it (if any). In your code above it returns void
as you have not supplied anything to return.
return
另一方面是编译器熟知的保留关键字。
return
返回一个值并结束当前函数的运行,将堆栈向上移回调用它的函数(如果有)。在您上面的代码中,它返回,void
因为您没有提供任何要返回的内容。
回答by Keerthivasan
What does return; mean?
返回什么;意思?
return;
really means it returns nothing void
. That's it.
return;
真的意味着它什么都不返回void
。就是这样。
why return; or other codes can write below the statement of System.exit(0);
为什么回来;或者其他代码可以写在System.exit(0)的语句下面;
It is allowed since compiler doesn't know calling System.exit(0)
will terminate the JVM
. The compiler will just give a warning - unnecessary return statement
这是允许的,因为编译器不知道调用System.exit(0)
会终止JVM
. 编译器只会给出警告 -不必要的 return 语句
回答by Karura91
System.exit() terminates the JVM. Nothing after System.exit() is executed. Return is generally used for exiting a method. If the return type is void, then you could use return; But I don't think is a good practice to do it in the main method. You don't have to do anything for terminate a program, unless infinite loop or some strange other execution flows.
System.exit() 终止 JVM。System.exit() 执行后没有任何内容。Return 通常用于退出方法。如果返回类型为 void,则可以使用 return; 但我认为在 main 方法中这样做不是一个好习惯。您不必为终止程序做任何事情,除非无限循环或其他一些奇怪的执行流程。
回答by anirudh
- Well, first System.exit(0) is used to terminate the program and having statements below it is not correct, although the compiler does not throw any errors.
- a plain
return;
is used in a method of void return type to return the control of execution to its parent method.
- 好吧,首先 System.exit(0) 用于终止程序,并且在它下面的语句是不正确的,尽管编译器没有抛出任何错误。
return;
在void返回类型的方法中使用plain将执行控制返回给其父方法。
回答by ReeCube
So return; does not really terminate your java program, it only terminates your java function (void). Because in your case the function is the main function of your application, return; will also terminate the application. But the return; in your example is useless, because the function will terminate directly after this return anyway...
所以返回;并没有真正终止您的 java 程序,它只是终止您的 java 函数 (void)。因为在您的情况下,该功能是您应用程序的主要功能,请返回;也会终止申请。但回报;在您的示例中是无用的,因为无论如何该函数将在返回后直接终止......
The System.exit() will completly terminate your programand it will close any open window.
该System.exit()的会完全地终止你的程序,它会关闭所有打开的窗口。
回答by prady00
- System.exit()is a function which cause JVM to exit.
- returnjust returns the control to calling function.
- return 8will return control and value 8 to calling function.
- System.exit()是一个导致 JVM 退出的函数。
- return只是将控制权返回给调用函数。
- return 8将控制和值 8 返回给调用函数。