为什么必须将`String [] args`作为main()参数? [英] Why is it necessary to have `String[] args` as main() parameter?
问题描述
我不问什么是(String args[])
,因为在这里得到了回答:.
I'm not asking what is (String args[])
because that was answered here: What is "String args[]"? parameter in main method Java.
我的问题是,为什么在编写main()时有必要编写它?
My question is why is it necessary to write it while writing main()?
我参加了实践考试,遇到了一个问题,意识到自己在写public static void main()
时没有写过String args[]
.但是然后在编写main(String args[])
之后,问题就解决了. (如何以及为什么我仍然不知道!)
I had my practical exams, where I faced a problem and realized I hadn't written String args[]
while writing public static void main()
. But then after writing main(String args[])
the problem was solved. (How and Why I still don't know!)
在同一天,我被问到Viva时,我被问到-写main()
时是否有必要写这个String args[]
?"并感谢发生的错误,我回答是",但在被问到为什么?"时却没有答案.
所以我想知道为什么需要写String [] args .
On that same day I was asked in Viva I was asked - "Is it necessary to write this String args[]
while writing main()
?" and thanks to the error that occurred I replied "YES" but was left without answer when asked "WHY?".
So I want to know why is it necessary to write String[] args.
推荐答案
来自
必须将方法 The method (请注意,您不能在同一个类中同时使用 (note that you can't have two 因此,当您执行类似命令时 So when you execute command like Java虚拟机会将 Java Virtual Machine will place 当命令没有诸如此类的任何参数时,也使用此方法 This method is also used when command doesn't have any arguments like 此决定简化了我们的生活,因为我们无需专注于处理有两个切入点的情况 This decision simplifies our life because we don't need to focus on handling cases where there are two entry points 我们可以简单地允许用户传递参数,但是如果我们不想处理它们,我们可以简单地忽略它们. We can simply allow user to pass arguments but if we don't wan to handle them we can simply ignore them. 还请记住,我们可以在类中使用任何具有正确声明的方法(并且不违反从超类继承的任何规则,如扩大成员可见性-我们不能将受保护的方法公开),因此没有任何方法拥有 Also remember that we are allowed to have in our class any method which has proper declaration (and doesn't violate any rules inherited from superclass like widening member visibility - we can't make protected method public), so there is nothing wrong with having 但是您需要意识到该方法不能用作入口点,因此,如果要从此方法启动应用程序,则需要创建适当的 But you need to realize that this method can't be used as entry point, so if you want to start your application from this method you will need to create proper 这篇关于为什么必须将`String [] args`作为main()参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!main
声明为public
,static
和void
.它必须指定一个声明的类型为String
数组的形式参数(第8.4.1节).因此,可以使用以下任何一种声明:
main
must be declared public
, static
, and void
. It must specify a formal parameter (§8.4.1) whose declared type is array of String
. Therefore, either of the following declarations is acceptable:public static void main(String[] args)
public static void main(String... args)
String[]
和String...
的两个main
方法,因为main
methods with String[]
and String...
in same class, since varargs are simply syntactic sugar which at compilation time will be replaced with arrays so you would end up with two methods handing String[]
and one class can't have two methods with same name and parameters)java YourClass foo bar
foo
和bar
参数放在String[]
数组中,并将尝试将该数组传递给main
方法,该方法可以接受它作为参数. foo
and bar
parameters in String[]
array and will try to pass that array to main
method which can accept it as parameter. java YourType
public static void main(){
/*your code*/
}
main
方法,该方法将执行您的 main()
方法:main
method which will execute your main()
method:public static void main(String ...){
main();
}