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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25225771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:37:31  来源:igfitidea点击:

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

javastringargumentsmain

提问by HENCE PROVED

why is it mandatory to pass string arg[] as an argument in main method? why we cannot pass any other data type available in java? whats the importance of passing String arg[] in main method in java?

为什么必须在 main 方法中将字符串 arg[] 作为参数传递?为什么我们不能传递java中可用的任何其他数据类型?在 java 的 main 方法中传递 String arg[] 的重要性是什么?

采纳答案by user949300

History. This is a convention since the days of C, maybe even earlier? Java took most of its syntax from C.

历史。这是自 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. Arrays did exist.

此外,命令行参数是字符串,这就是数据类型的原因。Java 1 中不存在集合,因此它们不是一个选项。数组确实存在。

回答by Am_I_Helpful

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. There can be several parameters!

因为通过传递String arrays,我们可以轻松地以字符串的形式传递所有必要的参数,例如与程序相关的选项/参数。可以有多个参数!

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!

使用多个参数调用程序的示例,因此将它们存储在字符串数组中!

java Sample_Example example1 example2 example3  

Arguments:

参数:

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

Here, you are calling Sample_ExampleClass and passing three parameters example1,example2and example3to be used by the program! 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. Always we tend to make our work simpler and here Java makes it simpler for all of us by providing this facility!

在这里,你在呼唤Sample_Example类和传递三个参数example1example2example3通过该程序可以使用!因此,将它们存储在 String 数组中总是更好的选择,而不是其他原始数据类型、集合或 Wrapper 数据类型。我们总是倾向于让我们的工作更简单,而在这里,Java 通过提供这个工具让我们所有人都变得更简单!

回答by Jon Newmuis

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 Argumentsin Oracle's Java tutorials for more details.

有关更多详细信息,请参阅Oracle 的 Java 教程中的命令行参数

回答by shabbir

when we run a java program to command prompt, we can pass some input to our Java program. Those inputs are stored in this String args array.

当我们运行 Java 程序到命令提示符时,我们可以将一些输入传递给我们的 Java 程序。这些输入存储在此 String args 数组中。

回答by HENCE PROVED

Because if also we are not passing any argument value while running the main method then also its working fine. It creates an empty string, when we are not passing any values to string arg[]. Where else in case of other data type we have to pass some values.

因为如果我们在运行 main 方法时也没有传递任何参数值,那么它也可以正常工作。当我们没有将任何值传递给字符串 arg[] 时,它会创建一个空字符串。在其他数据类型的情况下,我们必须传递一些值。