The System.exit() method terminates the Java virtual machine. This method takes an argument, which is the status code passed to the caller.

System.exit()方法终止Java虚拟机。 此方法采用一个参数,这是传递给调用方的状态代码。

A status code of zero signifies a normal exit whereas a status code of non-zero signifies an abnormal code.

状态代码为零表示正常退出,而状态代码为非零表示异常代码。

The code is not so important within Java itself. However, it’s common for scripts to rely on the exit codes of commands it invokes.

在Java本身中,代码并不是那么重要。 但是,脚本通常依赖于它调用的命令的退出代码。

Several non-zero codes can mean several different types of exits. This enables programmers to design their response system accordingly.

几种非零代码可能意味着几种不同类型的出口。 这使程序员可以相应地设计其响应系统。

The System.exit() is like a harsh version of breaking out of loops using conditionals.

System.exit()类似于使用条件语句打破循环的严酷版本。

System.exit(),状态码为0 (System.exit() with status code 0)

Let’s write a for loop and use System.exit(0) to exit out of the program with a status code of 0.

让我们编写一个for循环,并使用System.exit(0)以状态代码0退出程序。


public class Main {public static void main(String[] args) {for(int i=0;i<10;i++){System.out.println(i);if(i>5){System.exit(0);}}}
}
Output
输出量

0
1
2
3
4
5
6Process finished with exit code 0

We can see that the program terminates with an exit code of 0.

我们可以看到程序以退出代码0终止。

以非零状态码退出 (Exiting with a non-zero status code )

We can exit out of the program with any non zero exit code.

我们可以使用任何非零的退出代码退出程序。


public class Main {public static void main(String[] args) {for(int i=0;i<10;i++){System.out.println(i);if(i>5){System.exit(10);}}}
}

You can see that the exit code is printed as 10 in the output.

您可以看到出口代码在输出中显示为10。

何时使用System.exit() (When to use System.exit())

It’s useful when you are calling a Java program from shell script. You can terminate the program in case of an abnormal scenario and pass on the status code to the caller shell script.

从外壳程序脚本调用Java程序时,此功能很有用。 您可以在异常情况下终止程序,并将状态代码传递给调用方外壳脚本。

结论 (Conclusion )

That is how Java.lang.System.exit() works. It is common to use it to exit loops and functions. Interestingly enough, System.exit() if not placed properly can render a certain portion of your code unreachable.

这就是Java.lang.System.exit()的工作方式。 通常使用它退出循环和功能。 有趣的是,如果未正确放置System.exit(),可能会使代码的某些部分无法访问。

翻译自: https://www.journaldev.com/41960/system-exit-in-java

Java中的System.exit()相关推荐

  1. java system_深入分析java中的System

    System是一个类,这个System类主要是一些与系统相关的属性和方法的集合,而且其内部的方法全部是静态的,所以我们直接使用System直接调用就好,比如我们常用的一个System.out.prin ...

  2. Java中的System.out.println到底是什么,而且Java源码中System.java的out是null,为什么可以调用println方法?

    在我们接触Java第一个程序时,我们使用了public class和public static void main(String[]) args)以及System.out.println(" ...

  3. Java中的System.getProperty()设置参数的方法

    ■前言 今天调查代码时,发现有一行代码如下, System.getProperty("authPasswd")://Key值是随便写的,不过确实是一个密码字段 在代码中,没有发现任 ...

  4. Java中的System.getenv()和System.getProperty()

    System.getenv()和System.getProperty()是Java种常用的两种读取环境配置信息的方法,它们都是java.lang.System类的方法,用途有相似之处,但我们在实际使用 ...

  5. java中调用System.currentTimeMillis()获取当前时间来求出时间差及程序执行的时间

    package com.test;import java.util.Scanner; //String的连接1000次 class StringOperate{private String str;p ...

  6. java中的System.currentTimeMillis()是什么?时间的单位转换以及方法的使用

    在开发过程中,通常很多人都习惯使用new Date()来获取当前时间.new Date()所做的事情其实就是调用了System.currentTimeMillis().如果仅仅是需要或者毫秒数,那么完 ...

  7. java中的System.out.print()与System.out.println()的区别

    System.out.print();与System.out.println(); 前者输出不换行,比如: System.out.print("a"); System.out.pr ...

  8. 小技巧 ----- 关于Java中的System.arraycopy()

    使用方法: 传入5个参数:System.arraycopy(来源数组, 起始下标, 目标数组, 起始下标, 拷贝长度) 使用技巧: 数组的位移 通过该方法进行数组的位移:System.arraycop ...

  9. [转载] Java——System.exit()用法及个人理解

    参考链接: Java中的System.exit() 该方法的原型是:System.exit(int status). 取值及作用 status由使用者自取,一般可取0,1或者其他数:当其为System ...

最新文章

  1. 魔法引用函数magic_quotes_gpc和magic_quotes_runtime的区别和用法
  2. js编码java解码_js编码处理(转)
  3. numpy一维数组的反转/倒序
  4. Color the ball----HDOJ1556
  5. querySelector用法改进
  6. 今天整理了一些老作品
  7. jmeter跨线程组传多个值_Jmeter 跨线程组传递参数 之两种方法(转)
  8. 【无码专区12】子集和(背包dp)
  9. Windows编程之多媒体
  10. C#移除HTML标记
  11. 【PyQt5】连接 mysql 查询数据 并显示在 tableWidget 表格
  12. 吴恩达都在做的AI农业到底有什么魔力?
  13. std::sort使用
  14. c语言,10行代码写一个简单鼠标连点器
  15. Nansen:你想知道的关于 NFT 的一切
  16. FastDB使用记录
  17. Centos7 NAT网络连接方式以及NetworkManger进行网络连接管理
  18. VO、DTO、BO、PO、DO、POJO 数据模型的理解和实际使用
  19. Android屏幕共享-传输图片
  20. 【转载】秦九韶算法 Horner算法

热门文章

  1. 如何避免delete和delete[]的尴尬?
  2. [转]Android中在SurfaceView上高效绘图
  3. [转载] 扩展Python之在Python中调用C编写的函数模块
  4. [转载] HTML所有标签及其属性汇总
  5. verilog之门级相关知识
  6. ffmpeg下载rtmp flv
  7. 团队博客-11月15日
  8. ASP.NET2.0 分页控件 PagerPro.dll (1.1.0 最新)
  9. 数据结构笔记(二十)--二叉树的存储
  10. 《移动浪潮》读书笔记