This question already has an answer here:
这个问题在这里已有答案:
- What is “String args[]”? parameter in main method Java 13 answers
什么是“String args []”?主要方法中的参数Java 13回答
In the standard public static void main(String[] args)
, I have noticed that (at least in Dr. Java), you can use other words besides args
, but I have never seen any code that does deviate from this name.
在标准的public static void main(String [] args)中,我注意到(至少在Java博士中),你可以使用除args之外的其他单词,但我从未见过任何偏离此名称的代码。
My questions are
我的问题是
- Will using other words work in all versions of Java and development environments?
- Is the use of
args
just a convention? - If I use other names, will it be looked down upon?
使用其他词语是否适用于所有版本的Java和开发环境?
使用args只是一种惯例吗?
如果我使用其他名字,它会被低估吗?
Thank you for any help! I'm having trouble finding any answers online.
感谢您的任何帮助!我无法在网上找到任何答案。
3 个解决方案
#1
1
Yes the args variable is just convention. The variable name can be changed, but it is convention and you would be looked down upon. Source here.
是的,args变量只是约定。变量名称可以更改,但它是惯例,你会被忽视。来源于此。
#2
1
The docs at Oracle states, "You can name the argument anything you want, but most programmers choose "args" or "argv"."
甲骨文的文档说:“你可以根据需要命名论证,但大多数程序员选择”args“或”argv“。
#3
1
args
is short from arguments
and purpose of that array is to hold values provided when we start Java application like
args是参数的缩写,该数组的目的是保存我们启动Java应用程序时提供的值
java YourClass argument0 argument1 argument2
You can change name of any method parameter (compiler doesn't save those names anyway so they are only present at .java
file, and are replaced by other values at .class
file) including args
, but I would leave it as. As someone said:
您可以更改任何方法参数的名称(编译器不会保存这些名称,因此它们只出现在.java文件中,并被.class文件中的其他值替换)包括args,但我会将其保留为。有人说:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
总是编码好像最终维护你的代码的人将是一个知道你住在哪里的暴力精神病患者。
so don't do unnecessary changes.
所以不要做不必要的改变。
You can also remove that argument, but this way you will create your own separate method which will be called main
, but will no longer be entry point. This means you will not be able to run it directly via java YourClass
command because JVM would be searching for public static void main(String[])
method in YourClass
to start execution from there. But since your method will not fit this signature it wouldn't be treated as valid entry point.
您也可以删除该参数,但这样您将创建自己的单独方法,该方法将被称为main,但不再是入口点。这意味着您将无法通过java YourClass命令直接运行它,因为JVM将在YourClass中搜索public static void main(String [])方法以从那里开始执行。但由于您的方法不适合此签名,因此不会将其视为有效入口点。
#1
1
Yes the args variable is just convention. The variable name can be changed, but it is convention and you would be looked down upon. Source here.
是的,args变量只是约定。变量名称可以更改,但它是惯例,你会被忽视。来源于此。
#2
1
The docs at Oracle states, "You can name the argument anything you want, but most programmers choose "args" or "argv"."
甲骨文的文档说:“你可以根据需要命名论证,但大多数程序员选择”args“或”argv“。
#3
1
args
is short from arguments
and purpose of that array is to hold values provided when we start Java application like
args是参数的缩写,该数组的目的是保存我们启动Java应用程序时提供的值
java YourClass argument0 argument1 argument2
You can change name of any method parameter (compiler doesn't save those names anyway so they are only present at .java
file, and are replaced by other values at .class
file) including args
, but I would leave it as. As someone said:
您可以更改任何方法参数的名称(编译器不会保存这些名称,因此它们只出现在.java文件中,并被.class文件中的其他值替换)包括args,但我会将其保留为。有人说:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
总是编码好像最终维护你的代码的人将是一个知道你住在哪里的暴力精神病患者。
so don't do unnecessary changes.
所以不要做不必要的改变。
You can also remove that argument, but this way you will create your own separate method which will be called main
, but will no longer be entry point. This means you will not be able to run it directly via java YourClass
command because JVM would be searching for public static void main(String[])
method in YourClass
to start execution from there. But since your method will not fit this signature it wouldn't be treated as valid entry point.
您也可以删除该参数,但这样您将创建自己的单独方法,该方法将被称为main,但不再是入口点。这意味着您将无法通过java YourClass命令直接运行它,因为JVM将在YourClass中搜索public static void main(String [])方法以从那里开始执行。但由于您的方法不适合此签名,因此不会将其视为有效入口点。