终止Java程序 [英] Terminating a Java Program

查看:135
本文介绍了终止Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了终止(关闭或停止)Java程序的方法。我找到了两个解决方案。

I found out ways to terminate (shut-down or stop) my Java programs. I found two solutions for it.


  1. 使用返回;
    当我想要退出或终止我的程序执行,我添加了这个。

  1. using return;
    When I want to quit or terminate my program execution , I add this.

使用 System.exit();
有时我用它。我从上了解了 Sytem.exit()这个问题

using System.exit() ;
Sometimes I used it. I read about Sytem.exit() from This Question.

所以,我对它们都知之甚少。但我仍然对他们的实际工作方式感到困惑。请检查以下代码...

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;
 }
}

我确信 2 不会出现。我想知道为什么 return; 或其他代码可以写在 System.exit(0); 以及返回的真正定义是什么?因为对我来说很奇怪返回没有任何变量或值)?

I am sure that 2 will 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) ?

推荐答案

调用 System.exit(0)(或任何其他值)导致Java虚拟机退出,终止当前进程。您传递的参数将是 java 进程将返回操作系统的返回值。您可以从程序中的任何位置进行此调用 - 结果将始终相同 - JVM终止。由于这只是在 System 类中调用静态方法,编译器不知道它会做什么 - 因此不会抱怨无法访问的代码。

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.

return 语句只是中止当前方法的执行。它的字面意思是将控件返回给调用方法。如果该方法声明为 void (如在您的示例中),则您不需要指定值,因为您需要返回空隙。如果声明方法返回特定类型,则必须指定要返回的值 - 此值必须是指定类型。

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 只有当它在正在执行的主类的 main 方法内时才会退出程序。如果您尝试将代码放在其后,编译器将抱怨无法访问的代码,例如:

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:

public static void main(String... str) {
    System.out.println(1);
    return;
    System.out.println(2);
    System.exit(0);
}

不能用大多数编译器编译 - 产生无法访问的代码错误指向第二个 System.out.println 调用。

will not compile with most compiler - producing unreachable code error pointing to the second System.out.println call.

这篇关于终止Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆