简体   繁体   English

终止 Java 程序

[英]Terminating a Java Program

I found out ways to terminate (shut-down or stop) my Java programs.我找到了终止(关闭或停止)我的 Java 程序的方法。 I found two solutions for it.我找到了两个解决方案。

  1. using return;使用回报;
    When I want to quit or terminate my program execution , I add this.当我想退出或终止我的程序执行时,我添加了这个。

  2. using System.exit() ;使用System.exit() ;
    Sometimes I used it.有时我使用它。 I read about System.exit() from this question .我从这个问题中读到了System.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 2 will not appear.我相信2不会出现。 I would like to know is why return;我想知道是为什么return; or other codes can write below the statement of System.exit(0);或者其他代码可以写在System.exit(0);的语句下面System.exit(0); and what was real definition for return;什么是return;真正定义return; ( because it is strange thing for me return without any variables or values ) ? 因为对我来说return没有任何变量或值很奇怪)?

Calling System.exit(0) (or any other value for that matter) causes the Java virtual machine to exit, terminating the current process.调用System.exit(0) (或任何其他与此相关的值)会导致 Java 虚拟机退出,终止当前进程。 The parameter you pass will be the return value that the java process will return to the operating system.你传递的参数将是java进程返回给操作系统的返回值。 You can make this call from anywhere in your program - and the result will always be the same - JVM terminates.您可以从程序中的任何位置进行此调用 - 结果将始终相同 - JVM 终止。 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类中的静态方法,编译器不知道它将做什么 - 因此不会抱怨无法访问的代码。

return statement simply aborts execution of the current method. return语句只是中止当前方法的执行。 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 .如果该方法声明为void (如您的示例中所示),则您不需要指定值,因为您需要返回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 would cause the program to exit only if it's inside the main method of the main class being execute.仅当程序位于正在执行的主类的main方法内时, return才会导致程序退出。 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);
}

will not compile with most compiler - producing unreachable code error pointing to the second System.out.println call.不会使用大多数编译器进行编译 - 产生指向第二个System.out.println调用的unreachable code错误。

  1. System.exit() is a method that causes JVM to exit. System.exit()是一种导致 JVM 退出的方法。
  2. return just returns the control to calling function. return只是将控制权返回给调用函数。
  3. return 8 will return control and value 8 to calling method. return 8将控制和值 8 返回给调用方法。

Because System.exit() is just another method to the compiler.因为System.exit()只是编译器的另一种方法。 It doesn't read ahead and figure out that the whole program will quit at that point (the JVM quits).它不会提前阅读并确定整个程序将在那时退出(JVM 退出)。 Your OS or shell can read the integer that is passed back in the System.exit() method.您的操作系统或外壳程序可以读取在System.exit()方法中传回的整数。 It is standard for 0 to mean "program quit and everything went OK" and any other value to notify an error occurred. 0表示“程序退出并且一切正常”和任何其他值来通知发生错误是标准的。 It is up to the developer to document these return values for any users.由开发人员为任何用户记录这些返回值。

return on the other hand is a reserved key word that the compiler knows well.另一方面, return是编译器非常熟悉的保留关键字。 return returns a value and ends the current function's run moving back up the stack to the function that invoked it (if any). return返回一个值并结束当前函数的运行,将堆栈向上移动到调用它的函数(如果有)。 In your code above it returns void as you have not supplied anything to return.在您上面的代码中,它返回void因为您没有提供任何要返回的内容。

System.exit() terminates the JVM. System.exit() 终止 JVM。 Nothing after System.exit() is executed. System.exit() 执行后没有任何内容。 Return is generally used for exiting a method. Return 通常用于退出方法。 If the return type is void, then you could use return;如果返回类型为 void,则可以使用 return; But I don't think is a good practice to do it in the main method.但我认为在 main 方法中这样做不是一个好习惯。 You don't have to do anything for terminate a program, unless infinite loop or some strange other execution flows.您不必为终止程序做任何事情,除非无限循环或其他一些奇怪的执行流程。

So return;所以返回; does not really terminate your java program , it only terminates your java function (void) .并没有真正终止您的 java 程序它只是终止您的 java 函数 (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...在您的示例中是无用的,因为无论如何该函数将在返回后直接终止......

The System.exit() will completly terminate your program and it will close any open window. System.exit() 将完全终止您的程序并关闭所有打开的窗口。

  • 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.好吧,首先 System.exit(0) 用于终止程序并且在它下面的语句是不正确的,尽管编译器没有抛出任何错误。
  • a plain return;简单的return; is used in a method of void return type to return the control of execution to its parent method.用于void返回类型的方法中,将执行的控制权返回给其父方法。

What does return;返回什么; mean?意思?

return; really means it returns nothing void .真的意味着它不返回void That's it.就是这样。

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 .这是允许的,因为编译器不知道调用System.exit(0)将终止JVM The compiler will just give a warning - unnecessary return statement编译器只会给出警告 -不必要的 return 语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM