java (String[] arguments) 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17661389/
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 does (String[] arguments) mean?
提问by Tony Smith
I added the following code to a new class I created in Java:
我将以下代码添加到我用 Java 创建的新类中:
public static void main(String[] arguments) {
I understand what public, static and void mean, but what does (String[] arguments
) mean?
我明白 public、static 和 void 是什么意思,但是 ( String[] arguments
) 是什么意思?
回答by Ravi Thapliyal
Your main()
method can take input parameters of type String
if your program is run through a console like
你的main()
方法可以采取类型的输入参数String
,如果你的程序是通过像控制台运行
java YourClass arg1 arg2
Now, within main()
if you iterate the String []
like
现在,main()
如果你迭代String []
类似
for (arg : arguments)
System.out.println(arg);
it should print
它应该打印
arg1
arg2
Demo :
演示:
public class AddTwoNumbers {
public static void main(String[] args) {
if(args.length == 2) {
try {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.println("a + b = " + a + " + " + b + " = "+ (a + b));
} catch (NumberFormatException e) {
System.err.println("Invalid Input: Please enter numbers.");
}
} else {
System.err.println("Missing Input: Please enter TWO numbers.");
}
}
}
You can run this on your console as
你可以在你的控制台上运行它
java AddTwoNumbers 2 3
and it should print
它应该打印
a + b = 2 + 3 = 5
回答by Edwin Buck
It literally means "an array, where every element inside of it is at least a String, which we will name arguments".
它的字面意思是“一个数组,其中的每个元素至少是一个字符串,我们将其命名为参数”。
In the context of the rest of the line, the method main
takes as input, "an array, where every element inside of it is at least a String, which we will name arguments"
在该行其余部分的上下文中,该方法main
将“一个数组,其中的每个元素至少是一个字符串,我们将其命名为参数”作为输入
Since this is the public static void main(String[] arguments)
method call, there is a special "exception to the rule" of normal parameter passing. Methods that look like this are one of the very few timeswhen your input is not defined in some other part of your program. Instead the Java Virtual Machineconstructs the input into this method, from the command line arguments you gave the JVM.
由于这是public static void main(String[] arguments)
方法调用,因此普通参数传递有一个特殊的“规则例外”。看起来像这样的方法是您的输入未在程序的其他部分定义的极少数情况之一。相反,Java 虚拟机根据您提供给 JVM 的命令行参数构造此方法的输入。
回答by elias
It's the array of parameters that you may pass to your program during the execution. Ex:
它是您可以在执行期间传递给程序的参数数组。前任:
java YourClass param1 100 X
In your runtime, you'll have this array
在您的运行时,您将拥有这个数组
System.out.println(args.length); //prints 3
System.out.println(args[0]); //prints param1
System.out.println(args[1]); //prints 100
System.out.println(args[2]); //prints X
回答by Paul Renton
These are the parameters that the main function expects.
这些是主函数期望的参数。
String [] arguments is a java array of String objects. This means that the main function expects an array of Strings. This array of strings typically holds all command line parameter arguments passed in when the program is run from the command line.
String [] 参数是 String 对象的 java 数组。这意味着主函数需要一个字符串数组。这个字符串数组通常包含从命令行运行程序时传入的所有命令行参数参数。
From the command line
从命令行
java className stringOne stringTwo
In the programNote : means in .. So read this as for stringObject in arguments
在程序中注意:意味着在 .. 所以读这个作为参数中的 stringObject
for (stringObject : arguments) {
System.out.println(stringObject);
}
Or if you know the exact amount of Strings that will be in arguments is two then
或者,如果您知道参数中字符串的确切数量是两个,那么
System.out.println(arguments[0]);
System.out.println(arguments[1]);
Output->
输出->
stringOne
字符串一
stringTwo
字符串二
Hope this helps! Good luck learning Java
希望这可以帮助!祝你学习Java好运
回答by dataskills
It means that the function expects an array of strings.
这意味着该函数需要一个字符串数组。
回答by Juned Ahsan
String[] arguments
is the array for run time argument to your java program. If required you can pass arguemnts to your java program like this:
String[] arguments
是 java 程序的运行时参数数组。如果需要,您可以像这样将参数传递给您的 Java 程序:
java yourJavaMainClass args1 args2
In your java code you can use the arguments provided by simply iterating over this array.
在您的 java 代码中,您可以使用通过简单地迭代这个数组提供的参数。
arguments[0] // this should give you the args1
回答by Rupesh
Run the program using command line and the input provided there gets stored in obj array. So if you run the class as java className a1 a2
then object array will have a1 and a2 as elements.
使用命令行运行程序,那里提供的输入存储在 obj 数组中。因此,如果您按照以下方式运行该类,java className a1 a2
则对象数组将具有 a1 和 a2 作为元素。
Or if you are using eclipse IDE the go to run/debug config and do as shown
或者,如果您使用的是 Eclipse IDE,请转到运行/调试配置并按所示执行