Exacly(args.length> 0)是什么意思? [英] What Exacly (args.length>0) means?

查看:321
本文介绍了Exacly(args.length> 0)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对您来说可能很简单,但是由于我是java的新手,所以我想知道实际上是什么 接下来的部分呢?

This may be simple to you people but as i am new to java, so i want know actually what is going on in the following part?

if (args.length > 0) {
    file = args[0];
}


public class DomTest1 {
    public static void main(String[] args) {
        String file = "test1.xml";
        if (args.length > 0) {
            file = args[0];
        }
    }
}

推荐答案

这些称为命令行参数,您可以在程序中将其作为String数组获得.这是 Oracle教程

Those are called command line arguments , which you get as a String array in your program. Here is the Oracle tutorial

Java应用程序可以从命令行接受任意数量的参数.这样,用户可以在启动应用程序时指定配置信息.

A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched.

用户在调用应用程序时输入命令行参数,并在要运行的类的名称后指定它们.

The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run.

因此,下面的代码:

String file = "test1.xml";
if (args.length > 0) {
   file = args[0];
}

检查String[] args的长度是否大于0,这意味着它检查是否输入了任何命令行参数或数组为空.如果输入了命令行参数,则将file分配给该数组的第一个元素,否则将默认file分配给test1.xml.您可以通过以下方式运行课程:

Checks to see if the length of the String[] args is greater than 0 , which means it checks if any command line argument was entered or is the array empty. If command line arguments were entered , then assign file the first element of that array , or else default file to test1.xml. You can run your class as :

java DomTest1  someFileName.someExtension

启动应用程序时,运行时系统会通过字符串数组将命令行参数传递给应用程序的main方法.在上一个示例中,命令行参数在包含单个字符串的数组中传递给 DomTest1 应用程序:"someFileName.someExtension".

这篇关于Exacly(args.length> 0)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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