指定 main 方法接受的参数类型的目的是什么?
What is the purpose of specifying the type of argument a main method takes in?
public static void main (string[] args)
是一个 main 方法,必须将字符串数组作为参数。
但是,当我 运行 一个程序时,main 方法会自动 运行s 而无需我显式地使用参数调用它。
因此,我有以下疑问:
- main 方法的参数类型重要吗?
- 是否存在显式调用带有参数的 main 方法的情况?如果是这样,这种情况的例子是什么?
- 当然; JVM 只自动调用
main(String[])
。它专门用于从命令行获取字符串参数。
- 这只是另一种方法:虽然它几乎总是用作入口点,但没有技术原因不能像其他任何静态方法一样使用它。我不会,因为它是非典型的和非交流的。
In the Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args)
The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".
The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
The main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application.`
你可以多读一些here and the main Java documentation, which is very practical, is here。
关于你的第二个问题,是的,你可以这样做,但我不建议这样做。
Does the type of argument of a main method matter?
是的。 JVM 使用 public static main(String[] args)
作为一般 Java 应用程序的执行入口点。换句话说,这是 JVM 将执行从它自己的内部加载和初始化例程移交给您自己的字节码的地方。除非您在容器(例如 GlassFish)或框架(例如 JavaFX)中执行应用程序,否则您的代码将需要这个入口点。按照惯例,它是 public static main(String...)
.
Are there situations in which one would explicitly call the main
method with arguments? If so, what is an example of such a situation?
对main(...)
的争论主要来自两个地方:
命令行。当您从 shell 或 Windows cmd.exe
中的命令行启动 Java 应用程序时,您可以在要执行的 JAR 文件的名称后键入一些附加数据。这些附加数据被处理成一个字符串数组,传递给您的 main()
方法。
配置设置。大多数 IDE 允许您为项目指定命令行参数。您可以在此设置中将参数传递给入口点(当您构建和 运行 您的项目时),就像在命令行上一样。
正如 Dave 所提到的,您自己的代码不会调用 main(...)
方法本身。它实际上只是作为入口点。
public static void main (string[] args)
是一个 main 方法,必须将字符串数组作为参数。
但是,当我 运行 一个程序时,main 方法会自动 运行s 而无需我显式地使用参数调用它。
因此,我有以下疑问:
- main 方法的参数类型重要吗?
- 是否存在显式调用带有参数的 main 方法的情况?如果是这样,这种情况的例子是什么?
- 当然; JVM 只自动调用
main(String[])
。它专门用于从命令行获取字符串参数。 - 这只是另一种方法:虽然它几乎总是用作入口点,但没有技术原因不能像其他任何静态方法一样使用它。我不会,因为它是非典型的和非交流的。
In the Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args)
The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".
The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
The main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application.`
你可以多读一些here and the main Java documentation, which is very practical, is here。
关于你的第二个问题,是的,你可以这样做,但我不建议这样做。
Does the type of argument of a main method matter?
是的。 JVM 使用 public static main(String[] args)
作为一般 Java 应用程序的执行入口点。换句话说,这是 JVM 将执行从它自己的内部加载和初始化例程移交给您自己的字节码的地方。除非您在容器(例如 GlassFish)或框架(例如 JavaFX)中执行应用程序,否则您的代码将需要这个入口点。按照惯例,它是 public static main(String...)
.
Are there situations in which one would explicitly call the main method with arguments? If so, what is an example of such a situation?
对main(...)
的争论主要来自两个地方:
命令行。当您从 shell 或 Windows
cmd.exe
中的命令行启动 Java 应用程序时,您可以在要执行的 JAR 文件的名称后键入一些附加数据。这些附加数据被处理成一个字符串数组,传递给您的main()
方法。配置设置。大多数 IDE 允许您为项目指定命令行参数。您可以在此设置中将参数传递给入口点(当您构建和 运行 您的项目时),就像在命令行上一样。
正如 Dave 所提到的,您自己的代码不会调用 main(...)
方法本身。它实际上只是作为入口点。