为什么必须将`String [] args`作为main()参数? [英] Why is it necessary to have `String[] args` as main() parameter?

查看:86
本文介绍了为什么必须将`String [] args`作为main()参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不问什么是(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.

推荐答案

来自

必须将方法main声明为publicstaticvoid.它必须指定一个声明的类型为String数组的形式参数(第8.4.1节).因此,可以使用以下任何一种声明:

The method 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方法,因为

(note that you can't have two 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)

因此,当您执行类似命令时

So when you execute command like

java YourClass foo bar

Java虚拟机会将foobar参数放在String[]数组中,并将尝试将该数组传递给main方法,该方法可以接受它作为参数.

Java Virtual Machine will place foo and bar parameters in String[] array and will try to pass that array to main method which can accept it as parameter.

当命令没有诸如此类的任何参数时,也使用此方法

This method is also used when command doesn't have any arguments like

java YourType

此决定简化了我们的生活,因为我们无需专注于处理有两个切入点的情况

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

public static void main(){
    /*your code*/
}

但是您需要意识到该方法不能用作入口点,因此,如果要从此方法启动应用程序,则需要创建适当的main方法,该方法将执行您的 main()方法:

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 main method which will execute your main() method:

public static void main(String ...){
    main();
}

这篇关于为什么必须将`String [] args`作为main()参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆