java java中合法的main方法签名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13603445/
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
legal main method signature in java
提问by Ravi
class NewClass{
public static void main(String a){
System.out.print("Hello");
}
}
When I'm trying to execute above code, then it shows an error, main method not found. But when I changed public static void main(String a)
to public static void main(String... a)
or public static void main(String a[])
. Then, it works..!!
当我尝试执行上面的代码时,它显示一个错误,main method not found。但是,当我换public static void main(String a)
到public static void main(String... a)
或public static void main(String a[])
。然后,它起作用了.. !!
So, My question is how many different ways we can write legal main method signatureand what this signature public static void main(String... a)
means ?
所以,我的问题是我们可以用多少种不同的方式来编写合法的主要方法签名以及这个签名public static void main(String... a)
意味着什么?
回答by Andrzej Doyle
Simply because that's the requirement of Java.
仅仅因为这是Java的要求。
A main method/entry point to a program mustbe a method declared as public static void main(String[] args)
. Your method that was declared with a String
parameter was similar but not compatible.
程序的主方法/入口点必须是声明为 的方法public static void main(String[] args)
。您使用String
参数声明的方法类似但不兼容。
An array is not the same as a single String - if someone invoked Java with three command-line parameters, the JVM would create a three-element string array, and then how would it pass this into your method that only takes a single string?
数组与单个字符串不同 - 如果有人使用三个命令行参数调用 Java,JVM 将创建一个三元素字符串数组,然后它将如何将其传递给您只接受单个字符串的方法?
So in that case you were trying to launch a Java program based on a class that did not have an main methodto act as an entry point.
因此,在这种情况下,您试图基于没有 main 方法作为入口点的类启动 Java 程序。
(The reason why String...
works is because this is syntactic sugar for an array parameter, and compiles down to a method with the same signature.)
(之所以String...
有效,是因为这是数组参数的语法糖,并编译为具有相同签名的方法。)
回答by Ravi
Finally, i found the answer of my question in Sun Certified Programmer for Java 6book.
最后,我在Sun Certified Programmer for Java 6一书中找到了我的问题的答案。
First question was, how many different legal ways of using main method?
第一个问题是,使用 main 方法有多少种不同的合法方式?
Legal main method signatures are
合法的主要方法签名是
public static void main(String a[])
public static void main(String[] a)
public static void main(String... a)
what does
(String... a)
means ??
是什么
(String... a)
意思??
To declare a method using a var-args
parameter, we need to follow with an ellipsis(...)
then use space and then name of the array that will hold the parameter received. So, above term known as Variable Argumentand which means 0 to many.
要使用var-args
参数声明方法,我们需要跟在一个ellipsis(...)
then use 空间,然后是将保存接收到的参数的数组的名称。因此,上述术语称为变量参数,意思是0 到许多。
And, rules of using variable argument parameters is, must be the last parameter in the method signature and can have only one var-args in a method.
并且,使用可变参数参数的规则是,必须是方法签名中的最后一个参数,并且一个方法中只能有一个 var-args。
Eg:
例如:
void myfunc(String... a) //legal
void myfunc(String a...) //illegal
void myfunc(String a,int.. b) //legal
void myfunc(String... a,int b) //illegal
回答by Debobroto Das
Its default in java. java compiler expects an array of command line arguments. thats why you need to specify string args[] or String...
它在java中的默认值。java 编译器需要一个命令行参数数组。这就是为什么您需要指定字符串 args[] 或 String ...
回答by Juan L. Restituyo
public static void main(String... a) --> Is a method with a variable argument list, that is internally(in the method) treated as an array.
public static void main(String... a) --> 是一个带有可变参数列表的方法,它在内部(在方法中)被视为一个数组。
Legal Main Method Signature:
合法的主要方法签名:
public static void main(String a[])
public static void main(String a[])
static public void main(String a[])
static public void main(String a[])
public static void main(String[] a)
public static void main(String[] a)
static public void main(String[] a)
static public void main(String[] a)
public static void main(String... a)
public static void main(String... a)
static public void main(String... a)
static public void main(String... a)
回答by Vamsi Sangam
All these are valid/legal declarations of the main function in Java.
所有这些都是 Java 中 main 函数的有效/合法声明。
public static void main(String[] args) {
// code
}
static public void main(String[] args) {
// code
}
static public void main(String args[]) {
// code
}
public static void main(String[] MarkElliotZuckerberg) {
// code
}
public static void main(String... NewYork) {
// code
}
The points to remember is -
要记住的要点是——
- The keywords public and static are interchangeable but mandatory.
- The parameter of the main method can take the var-args syntax.
- The name can be anything..!
- 关键字 public 和 static 可以互换但必须使用。
- main 方法的参数可以采用 var-args 语法。
- 名字可以是任何东西..!
Just for practice.. :P ...These are examples of invalid main method declarations -
仅供练习.. :P ...这些是无效 main 方法声明的示例 -
static void main(String[] args) {
// public is missing
}
public void main(String args[]) {
// static is missing
}
public static int main(String... Java) {
// return type not void
return 0;
}
public void Main(String args[]) {
// "main" not "Main"
}
public void main(string args[]) {
// "String" not "string"
}
public void main(String.. SayHi) {
// Ellipses is 3 dots !
}
Some give errors, while others simply overload the main method... Hope this helps...! If it did, let me know by commenting..!
有些给出错误,而另一些只是重载了主要方法......希望这会有所帮助......!如果是这样,请通过评论告诉我..!
Source - Java Tutorials on Theory of Programming
源代码 - Java 编程理论教程
回答by Narendra Pathai
Java Runtime tries to find a method with name "main" with argument types "String[]". It is just like using reflection for finding a method with type arguments as String[].
Java 运行时尝试查找名称为“main”且参数类型为“String[]”的方法。这就像使用反射来查找类型参数为 String[] 的方法一样。
Also String[] are used so that the runtime can pass the program arguments or Command Line arguments that are provided. It is like the runtime tokenizes the arguments with white space characters and then calls this method named "main".
还使用 String[] 以便运行时可以传递提供的程序参数或命令行参数。这就像运行时用空格字符标记参数,然后调用这个名为“main”的方法。
The method main must be declared public, static, and void. It must accept a single argument that is an array of strings. This method can be declared as either
方法 main 必须声明为 public、static 和 void。它必须接受作为字符串数组的单个参数。这个方法可以声明为
public static void main(String[] args)
or
public static void main(String... args)
You can also refer to Oracle Java specification documentation for more understanding
也可以参考Oracle Java 规范文档进一步了解
回答by Juvanis
public static void main(String a[])
is the main entry point signaturefor a typical Java program. So you should get on with this method signature.
public static void main(String a[])
是典型 Java 程序的主要入口点签名。所以你应该继续使用这个方法签名。
回答by rlegendi
You need exactly the String[] args
parameter (it's an array).
您需要确切的String[] args
参数(它是一个数组)。
The reason is that you need to declare the main()
method with a specified signature(and a method signature contains its name, number of its parameters andthe types of the parameters).
其原因是,你需要声明main()
一个方法指定的签名(和方法签名包含其名称,参数的数量和类型的参数)。
So if you create a function which a different parameter type (String vs. String array), it is not recognized.
因此,如果您创建一个具有不同参数类型(字符串与字符串数组)的函数,则无法识别它。