什么是“ String args []”? 主方法Java中的参数

本文翻译自:What is “String args[]”? parameter in main method Java

I'm just beginning to write programs in Java. 我刚刚开始用Java编写程序。 What does the following Java code mean? 以下Java代码是什么意思?

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

  • When would you use these args ? 您什么时候使用这些args

Source code and/or examples are preferred over abstract explanations 源代码和/或示例优先于抽象解释


#1楼

参考:https://stackoom.com/question/3jmQ/什么是-String-args-主方法Java中的参数


#2楼

in public static void main(String args[]) args is an array of console line argument whose data type is String. in public static void main(String args []) args是一个控制台行参数数组,其数据类型为String。 in this array, you can store various string arguments by invoking them at the command line as shown below: java myProgram Shaan Royal then Shaan and Royal will be stored in the array as arg[0]="Shaan"; 在此数组中,可以通过在命令行上调用它们来存储各种字符串参数,如下所示: java myProgram Shaan Royal,然后,Shaan和Royal将存储为arg [0] =“ Shaan”; arg[1]="Royal"; arg [1] =“皇家”; you can do this manually also inside the program, when you don't call them at the command line. 您可以在不通过命令行调用它们的情况下,也可以在程序内部手动进行此操作。


#3楼

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. 完成代码后,将其转换为扩展名为.java的文件,可以通过双击该文件来运行该文件,也可以在整个控制台(在Mac上为终端,在Windows上为cmd.exe)中运行该文件。许多事情,许多东西。 One thing is they can see console messages (System.out.print or System.out.println) which they can't see if they double click. 一件事是他们可以看到控制台消息(System.out.print或System.out.println),如果双击它们就看不到。 Another thing they can do is specify parameters, so normally you would use the line 他们可以做的另一件事是指定参数,因此通常您会使用该行

java -jar MyCode.jar

after navigating to the folder of the program with 在导航到该程序的文件夹后

cd C:My/Code/Location

on windows or 在Windows上或

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. 这些参数存储在args数组中,您可以在程序中使用该参数,以便允许用户控制特殊参数,例如要使用的文件或程序可以拥有的内存量。 If you want to know how to use an array, you could probably find a topic on this site or just google it. 如果您想知道如何使用数组,则可以在此站点上找到一个主题,或者只是在Google上搜索它。 Note that any number of parameters can be used. 注意,可以使用任意数量的参数。


#4楼

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. 我认为上面的答案已经很好地涵盖了String args[]只是一个字符串参数数组,您可以在运行它时将其传递给应用程序。 For completion, I might add that it's also valid to define the method parameter passed to the main method as a variable argument (varargs) of type String: 为了完成,我可能会补充说,将传递给main方法的方法参数定义为String类型的变量参数(varargs)也很有效:

public static void main (String... args)

In other words, the main method must accept either a String array ( String args[] ) or varargs ( String... args ) as a method argument. 换句话说, main方法必须接受String数组( String args[] )或varargs( String... args )作为方法参数。 And there is no magic with the name args either. args也不是魔术。 You might as well write arguments or even freddiefujiwara as shown in below e.gs.: 你还不如写arguments甚至freddiefujiwara如下图所示:e.gs

public static void main (String[] arguments)

public static void main (String[] freddiefujiwara)


#5楼

The style dataType[] arrayRefVar is preferred. 首选dataType[] arrayRefVar样式。 The style dataType arrayRefVar[] comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers. dataType arrayRefVar[]样式来自C / C ++语言,在Java中已采用,以适应C / C ++程序员。


#6楼

Explanation in simple layman's language. 用简单的外行语言解释。

The main method expects us to provide some arguments when we direct our JVM to the class name. 当我们将JVM指向类名时,main方法期望我们提供一些参数。 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. 这意味着,假设您的文件名为Try.java,现在要在命令提示符下执行此操作,请先编写“ javac Try.java”进行编译,然后再编写“ java Try”进行执行。 Now suppose instead of writing simply "java Try" you write "java Try 1". 现在,假设您不是简单地编写“ java Try”,而是编写“ java Try 1”。 Here you have passed an argument "1". 在这里,您传递了一个参数“ 1”。 This will be taken by your main method even if you don't use it in your code. 即使您的代码中未使用它,也将由您的main方法采用。

If you want to check whether your main method has actually taken the argument "1" or not. 如果要检查您的main方法是否实际采用了参数“ 1”。 Simply, inside your main method type the following: 只需在您的主要方法中键入以下内容:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值