java 字符串args[]在main方法中有什么用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3274612/
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-10-30 01:06:43  来源:igfitidea点击:

what is the use of string args[] in main method

java

提问by Govindarajulu

Possible Duplicate:
What is “String args[]”? in Java

可能的重复:
什么是“String args[]”?在 Java 中

What is the purpose of this method in java. If we not use what happens? If i give some array size then what happens

这个方法在java中的目的是什么?如果不使用会发生什么?如果我给出一些数组大小,那么会发生什么

回答by shookster

It's for passing in command line arguments: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/essential/environment/cmdLineArgs.html

它用于传递命令行参数:http: //download.oracle.com/docs/cd/E17409_01/javase/tutorial/essential/environment/cmdLineArgs.html

I think you have to have it declared in the main method (but you don't have to use it). I don't think you can give it an array size.

我认为您必须在 main 方法中声明它(但您不必使用它)。我不认为你可以给它一个数组大小。

回答by bluesmoon

The args array contains all command line parameters passed in to your Java application. For example, if you call your application like this:

args 数组包含传递给 Java 应用程序的所有命令行参数。例如,如果您这样调用您的应用程序:

java foo arg1 arg2 22 3 + 5

Then args[] will be an array of the following strings:

然后 args[] 将是以下字符串的数组:

"arg1", "arg2", "22", "3", "+", "5"

You can give it any name you want -- it doesn't have to be called args, but you cannot specify a size. This is so because you're not defining the parameter, you're merely specifying its type. Its size is determined based on the number of command line parameters passed in.

你可以给它任何你想要的名字——它不必被调用args,但你不能指定大小。这是因为您没有定义参数,您只是指定了它的类型。它的大小是根据传入的命令行参数的数量确定的。

回答by Stephen C

What is the use of string args[] in main method?

字符串 args[] 在 main 方法中有什么用?

It passes any command line arguments to your Java application. For example, if you run your app as:

它将任何命令行参数传递给您的 Java 应用程序。例如,如果您将应用程序运行为:

java -cp ... com.example.app1.App1 1 2 3 4

then the JVM tries to execute com.example.app1.App1.main(String[]), with an argument equivalent to that created by the expression new String[]{"1", "2", "3", "4"}.

然后 JVM 尝试执行com.example.app1.App1.main(String[]),并使用与表达式创建的参数等效的参数new String[]{"1", "2", "3", "4"}

What is the purpose of this method in java. If we not use what happens?

这个方法在java中的目的是什么?如果不使用会发生什么?

It provides an entry pointthat allows you to run the application from the command line. If you don't supply a suitable mainmethod, you'll be able to compile the application, but not run it. If you supply a mainmethod without the String[]argument, it won't be recognized as an entry point.

它提供了一个入口点,允许您从命令行运行应用程序。如果您不提供合适的main方法,您将能够编译应用程序,但不能运行它。如果您提供main不带String[]参数的方法,它将不会被识别为入口点。

If i give some array size then what happens?

如果我给出一些数组大小,那么会发生什么?

You get a Java compilation error; e.g.

你得到一个 Java 编译错误;例如

public static void main(String[10] args) { ...

is invalid Java. An array type declaration does not have a size, because the size is not part of an array type. The size of a Java array is a runtime attribute that is only determined when the array instance is created.

是无效的 Java。数组类型声明没有大小,因为大小不是数组类型的一部分。Java 数组的大小是一个运行时属性,仅在创建数组实例时确定。

回答by JesperE

The argsparameter contains the command line arguments passed to the JVM when starting your program, analogous to the argvparameter to the C/C++ mainfunction. You can declare it either as a String[]or as a varargs String....

args参数包含启动程序时传递给 JVM 的命令行参数,类似于argvC/C++main函数的参数。您可以将其声明为 aString[]或 varargs String...

The Java Language Specificationdoesn't seem to define the semantics of args, so I presume it is up to the JVM itself to determine how to populate the array. I can imagine that platforms such as Android can have other ways of populating the array.

Java语言规范似乎并没有界定args来语义,所以我相信它是由JVM本身来确定如何填充数组。我可以想象像 Android 这样的平台可以有其他方式来填充数组。

回答by sushil bharwani

Passing arguments from the command Line. Which can be used by the program as a set of parameters to operate upon or produce a result based on a set of known parameters.

从命令行传递参数。程序可以将其用作一组参数,以根据一组已知参数进行操作或产生结果。