java Exacly (args.length>0) 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17423257/
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
What Exacly (args.length>0) means?
提问by Bitopan
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?
这对你们来说可能很简单,但因为我是 Java 新手,所以我想知道接下来的部分实际上发生了什么?
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];
}
}
}
回答by NINCOMPOOP
Those are called command line arguments , which you get as a String array in your program. Here is the Oracle tutorial
这些称为命令行参数,您可以在程序中将其作为字符串数组获取。这是Oracle教程
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.
Java 应用程序可以从命令行接受任意数量的参数。这允许用户在启动应用程序时指定配置信息。
用户在调用应用程序时输入命令行参数,并在要运行的类的名称后指定它们。
Hence the below code :
因此下面的代码:
String file = "test1.xml";
if (args.length > 0) {
file = args[0];
}
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 :
检查 的长度String[] args
是否大于0
,这意味着它检查是否输入了任何命令行参数或数组是否为空。如果输入了命令行参数,则分配file
该数组的第一个元素,否则默认file
为test1.xml
. 您可以将类运行为:
java DomTest1 someFileName.someExtension
When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the DomTest1application in an array that contains a single String: "someFileName.someExtension".
当应用程序启动时,运行时系统通过字符串数组将命令行参数传递给应用程序的 main 方法。在前面的示例中,命令行参数以包含单个字符串的数组形式传递给DomTest1应用程序:“someFileName.someExtension”。
回答by Suresh Atta
args is an array of Command Line arguments
args 是命令行参数数组
When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings
当应用程序启动时,运行时系统通过字符串数组将命令行参数传递给应用程序的 main 方法
Where args
is an arrayand if (args.length > 0)
is the condition cheking that array is empty or not .
where args
是一个数组, if (args.length > 0)
是检查数组是否为空的条件。
回答by SatyamChaudhary
You are making String reference here and put the value in it. You first value is> test1.xml. It is a name of a file but you are putting into String as String(It means "test1.xml"). and then taking value from command line argument. And overriding value of you string reference by command line location 0. so you reference value will be always command line 0 location value if you do not pass any value then it will give you text1.xml
您正在此处进行 String 引用并将值放入其中。您的第一个值是> test1.xml。它是一个文件名,但您将字符串作为字符串放入(意思是“test1.xml”)。然后从命令行参数中获取值。并通过命令行位置 0 覆盖字符串引用的值。因此,如果您不传递任何值,则引用值将始终为命令行 0 位置值,那么它将为您提供 text1.xml
回答by WaterRocket8236
The main()
method is where the execution of java program starts.The place where all parameters passed to main() method are String args[]
. It is basically a String array. The variable name can be changed to something else other than using only args
, You may use String var[]
or `String datas[] or something else.
该main()
方法是其中其中所有参数传递给main()方法的java程序starts.The地方的执行是字符串args[]
。它基本上是一个字符串数组。变量名称可以更改为其他内容,而不是使用 only args
,您可以使用 Stringvar[]
或 `String datas[] 或其他内容。
Now, Coming to the if
condition checking in your program if (args.length > 0)
.
I will explain the fundamental of why arg.length
is so.
现在,进入程序中的if
条件检查if (args.length > 0)
。我将解释为什么arg.length
会这样的根本原因。
When a java program is executed from command line or similar terminal, it is run as java customName. Let's say the parameters you want to pass to java program as java customNameparam1 param2. The arguments are passed along with command line.Now the interpreter in java interprets these paramenters(i.e param1 param2) and passes them to main() method of the program. These parameters are stored in the args[]
String Array.
当从命令行或类似终端执行 java 程序时,它会作为 java customName运行。假设您要作为 java customNameparam1 param2传递给 java 程序的参数。参数与命令行一起传递。现在java中的解释器解释这些参数(即param1 param2)并将它们传递给程序的main()方法。这些参数存储在args[]
字符串数组中。
Now while running java program args[0] and args[1] will be allowed.If no arguments are passed then the value of args[] will be null and will be still be identified as String array object with null parameters(no elements).
In that case the args.length
will be equal to 0.
现在在运行 java 程序时,将允许 args[0] 和 args[1]。如果没有传递参数,则 args[] 的值将为空,并且仍将被标识为具有空参数(无元素)的 String 数组对象. 在这种情况下,args.length
将等于 0。
回答by duffymo
That line is checking to see if arguments were actually entered on the command line.
该行正在检查是否在命令行中实际输入了参数。
If any are entered, the first one is the name of the file.
如果有任何输入,第一个是文件名。
If none are entered, test1.xml
is the default.
如果未输入,test1.xml
则为默认值。
回答by Ganesh Rengarajan
The args.length value is the number of items in the args array.
args.length 值是 args 数组中的项目数。
If you pass no command-line arguments, you will always get "There are 0 command line arguments".
如果不传递命令行参数,您将始终得到“有 0 个命令行参数”。
thats why you checking
这就是为什么你检查
if (args.length > 0)
But try running the program like this: java PrintArgs hello my name is mikki2 The words after java PrintArgs are called command line arguments because they are arguments passed to your program from the command line
但是尝试像这样运行程序: java PrintArgs hello my name is mikki2 java PrintArgs 之后的词称为命令行参数,因为它们是从命令行传递给您的程序的参数