什么是“字符串参数[]”?Java主方法中的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/890966/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:44:50  来源:igfitidea点击:

What is "String args[]"? parameter in main method Java

javaparameterscommand-line-argumentsmain

提问by freddiefujiwara

I'm just beginning to write programs in Java. What does the following Java code mean?

我刚刚开始用 Java 编写程序。下面的Java代码是什么意思?

public static void main(String[] args)
  • What is String[] args?

  • When would you use these args?

  • 什么是String[] args

  • args什么时候会使用这些?

Source code and/or examples are preferred over abstract explanations

源代码和/或示例优于抽象解释

回答by mipadi

argscontains the command-line arguments passed to the Java program upon invocation. For example, if I invoke the program like so:

args包含在调用时传递给 Java 程序的命令行参数。例如,如果我像这样调用程序:

$ java MyProg -f file.txt

Then argswill be an array containing the strings "-f"and "file.txt".

然后args将是一个包含字符串"-f"和的数组"file.txt"

回答by Dan Lew

In Java argscontains the supplied command-line argumentsas an array of Stringobjects.

在 Java 中,args包含提供的命令行参数作为String对象数组。

In other words, if you run your program as java MyProgram one twothen argswill contain ["one", "two"].

换句话说,如果你运行你的程序,java MyProgram one two那么args将包含["one", "two"].

If you wanted to output the contents of args, you can just loop through them like this...

如果你想输出 的内容args,你可以像这样循环遍历它们......

public class ArgumentExample {
    public static void main(String[] args) {
        for(int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }
}

回答by Cuga

String [] argsis also how you declare an array of Strings in Java.

String [] args这也是在 Java 中声明字符串数组的方式。

In this method signature, the array argswill be filled with values when the method is called (as the other examples here show). Since you're learning though, it's worth understanding that this argsarray is just like if you created one yourself in a method, as in this:

在此方法签名中,args调用该方法时将用值填充数组(如此处的其他示例所示)。由于您正在学习,因此值得理解的是,此args数组就像您在方法中自己创建的一样,如下所示:

public void foo() {
    String [] args = new String[2];
    args[0] = "hello";
    args[1] = "every";

    System.out.println("Output: " + args[0] + args[1]);

    // etc... the usage of 'args' here and in the main method is identical
}

回答by shsteimer

When a java class is executed from the console, the main method is what is called. In order for this to happen, the definition of this main method must be

从控制台执行java类时,调用的是main方法。为了发生这种情况,这个主要方法的定义必须是

public static void main(String [])

The fact that this string array is called args is a standard convention, but not strictly required. You would populate this array at the command line whne you invoke your program

这个字符串数组被称为 args 是一个标准约定,但不是严格要求的。您将在调用程序时在命令行中填充此数组

java MyClass a b c

These are commonly used to define options of your program, for example files to write to or read from.

这些通常用于定义程序的选项,例如要写入或读取的文件。

回答by mR_fr0g

Those are for command-line arguments in Java.

这些用于 Java 中的命令行参数。

In other words, if you run

换句话说,如果你跑

java MyProgram one two

java MyProgram 一二

Then argscontains:

然后args包含:

[ "one", "two" ]

[ “一二” ]

public static void main(String [] args) {
    String one = args[0]; //=="one"
    String two = args[1]; //=="two"
}

The reason for this is to configure your application to run a particular way or provide it with some piece of information it needs.

这样做的原因是将您的应用程序配置为以特定方式运行或为其提供所需的某些信息。



If you are new to Java, I highly recommend reading through the official Oracle's JavaTMTutorials.

如果您是 Java 新手,我强烈建议您阅读官方 Oracle 的Java TM教程

回答by Shaan Royal

in public static void main(String args[])args is an array of console line argument whose data type is String. in this array, you can store various string arguments by invoking them at the command line as shown below: java myProgram Shaan Royalthen Shaan and Royal will be stored in the array as arg[0]="Shaan"; arg[1]="Royal"; you can do this manually also inside the program, when you don't call them at the command line.

public static void main(String args[])args 是控制台行参数的数组,其数据类型为字符串。在这个数组中,您可以通过在命令行调用它们来存储各种字符串参数,如下所示: java myProgram Shaan Royal然后 Shaan 和 Royal 将作为 arg[0]="Shaan"; 存储在数组中;arg[1]="皇家"; 当您不在命令行中调用它们时,您也可以在程序内部手动执行此操作。

回答by Zac

When you finish your code, you will turn it into a file with the extension .java, which can be run by double clicking it, but also throughout a console (terminal on a mac, cmd.exe on windows) which lets the user do many things. One thing is they can see console messages (System.out.print or System.out.println) which they can't see if they double click. Another thing they can do is specify parameters, so normally you would use the line

完成代码后,您会将其转换为扩展名为 .java 的文件,该文件可以通过双击运行,但也可以通过控制台(Mac 上的终端,Windows 上的 cmd.exe)让用户执行很多东西。一件事是他们可以看到控制台消息(System.out.print 或 System.out.println),如果双击则无法看到这些消息。他们可以做的另一件事是指定参数,因此通常您会使用该行

java -jar MyCode.jar

after navigating to the folder of the program with

导航到程序的文件夹后

cd C:My/Code/Location

on windows or

在窗户上或

cd My/Code/Location

on Mac (notice that mac is less clunky) to run code, but to specify parameters you would use

在 Mac 上(注意 mac 不那么笨重)运行代码,但要指定您将使用的参数

java -jar MyCode.jar parameter1 parameter2

These parameters stored in the args array, which you can use in your program is you want to allow the user to control special parameters such as what file to use or how much memory the program can have. If you want to know how to use an array, you could probably find a topic on this site or just google it. Note that any number of parameters can be used.

这些参数存储在 args 数组中,您可以在您的程序中使用它们是您希望允许用户控制特殊参数,例如使用什么文件或程序可以拥有多少内存。如果你想知道如何使用数组,你可能会在这个网站上找到一个主题,或者只是谷歌一下。请注意,可以使用任意数量的参数。

回答by sedeh

I think it's pretty well covered by the answers above that String args[]is simply an array of string arguments you can pass to your application when you run it. For completion, I might add that it's also valid to define the method parameter passed to the mainmethod as a variable argument (varargs) of type String:

我认为上面的答案很好地涵盖了它String args[]只是一个字符串参数数组,您可以在运行它时传递​​给您的应用程序。为了完成,我可能会补充说,将传递给main方法的方法参数定义为String 类型的可变参数 (varargs)也是有效的:

public static void main (String... args)

public static void main (String... args)

In other words, the mainmethod must accept either a String array (String args[]) or varargs (String... args) as a method argument. And there is no magic with the name argseither. You might as well write argumentsor even freddiefujiwaraas shown in below e.gs.:

换句话说,该main方法必须接受 String 数组 ( String args[]) 或 varargs ( String... args) 作为方法参数。而且这个名字args也没有什么魔力。您不妨编写arguments甚至freddiefujiwara如下所示,例如:

public static void main (String[] arguments)

public static void main (String[] arguments)

public static void main (String[] freddiefujiwara)

public static void main (String[] freddiefujiwara)

回答by Gerald

The style dataType[] arrayRefVaris preferred. The style dataType arrayRefVar[]comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.

风格dataType[] arrayRefVar是首选。该样式dataType arrayRefVar[]来自 C/C++ 语言,并在 Java 中采用以适应 C/C++ 程序员。

回答by Jatin Lalwani

Explanation in simple layman's language.

用通俗易懂的语言解释。

The main method expects us to provide some arguments when we direct our JVM to the class name. That means, suppose your file name is Try.java, now to execute this in command prompt you write "javac Try.java" to compile followed by "java Try" to execute. Now suppose instead of writing simply "java Try" you write "java Try 1". Here you have passed an argument "1". This will be taken by your main method even if you don't use it in your code.

当我们将 JVM 定向到类名时,main 方法希望我们提供一些参数。这意味着,假设您的文件名是 Try.java,现在要在命令提示符下执行它,您需要编写“javac Try.java”进行编译,然后编写“java Try”来执行。现在假设不是简单地写“java Try”,而是写“java Try 1”。在这里,您传递了一个参数“1”。即使您没有在代码中使用它,这也会被您的 main 方法采用。

If you want to check whether your main method has actually taken the argument "1" or not. Simply, inside your main method type the following:

如果您想检查您的主要方法是否实际采用了参数“1”。简单地,在您的主要方法中键入以下内容:

for(int i = 0; i < args.length; i++) {
        System.out.println("Argument is: "+args[i]);
    }