简体   繁体   English

为什么我们将String数组作为参数传递给main()方法,为什么不是任何集合类型或包装类型或基本类型?

[英]Why we Pass String array as argument to main() method, why not any collection type or wrapper type or primitive type?

why is it mandatory to pass string arg[] as an argument in main method? 为什么在main方法中将字符串arg []作为参数传递是必须的? why we cannot pass any other data type available in java? 为什么我们不能传递java中可用的任何其他数据类型? whats the importance of passing String arg[] in main method in java? 在java中的main方法中传递String arg []的重要性是什么?

History. 历史。 This is a convention since the days of C, maybe even earlier? 这是C以来的惯例,甚至可能更早? Java took most of its syntax from C. Java从C语言中获取了大部分语法。

Also, command line arguments are Strings which is why that is the data type. 此外,命令行参数是字符串,这就是数据类型的原因。 Collections did not exist in Java 1 so they were not an option. Java 1中不存在集合,因此它们不是一个选项。 Arrays did exist. 数组确实存在。

Because by passing String arrays , we can pass all the necessary parameters like options/arguments related to the program in the form of String easily. 因为通过传递String arrays ,我们可以轻松地以String的形式传递所有必要的参数,例如与程序相关的选项/参数。 There can be several parameters! 可以有几个参数!

Also, all the other datatypes can be easily converted from String! 此外,所有其他数据类型都可以从String轻松转换!

An example of calling a program with several parameters therefore resulting in storage of them in String array! 调用具有多个参数的程序的示例因此导致将它们存储在String数组中!

java Sample_Example example1 example2 example3  

Arguments: 参数:

  args[0]=example1
  args[1]=example2
  args[2]=example3 

Here, you are calling Sample_Example Class and passing three parameters example1 , example2 and example3 to be used by the program! 在这里,您调用Sample_Example Class并传递三个参数example1example2example3以供程序使用! So, it is always an better alternative to store them in an String array rather than other primitive data-types or in collections or in Wrapper data-types. 因此,将它们存储在String数组中而不是其他原始数据类型或集合或Wrapper数据类型中总是更好的替代方法。 Always we tend to make our work simpler and here Java makes it simpler for all of us by providing this facility! 总是我们倾向于使我们的工作更简单,这里Java通过提供这种设施使我们所有人都更加简单!

The idea is that your Java application will be invoked via a command (whether explicitly entered by a user, implicitly entered (like when you click a shortcut in your OS's GUI), etc). 我们的想法是,您的Java应用程序将通过命令调用(无论是由用户显式输入,是否隐式输入(例如,当您单击操作系统GUI中的快捷方式时)等)。

The String[] refers to the command line arguments specified when your application was invoked. String[]指的是在调用应用程序时指定的命令行参数。

See Command-Line Arguments in Oracle's Java tutorials for more details. 有关更多详细信息,请参阅Oracle Java教程中的命令行参数

when we run a java program to command prompt, we can pass some input to our Java program. 当我们运行java程序到命令提示符时,我们可以将一些输入传递给我们的Java程序。 Those inputs are stored in this String args array. 这些输入存储在此String args数组中。

Because if also we are not passing any argument value while running the main method then also its working fine. 因为如果我们在运行main方法时也没有传递任何参数值,那么它的工作正常。 It creates an empty string, when we are not passing any values to string arg[]. 当我们没有将任何值传递给字符串arg []时,它会创建一个空字符串。 Where else in case of other data type we have to pass some values. 在其他数据类型的情况下,我们必须传递一些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM