我们可以通过多少种不同的方式在 Java 中声明 main 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13620604/
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
In how many different ways can we declare a main method in java?
提问by Adithya
I how many different ways can we declare a main method in java?
我们可以用多少种不同的方式在 java 中声明一个 main 方法?
class A{
public static void main(String args[]){
System.out.println("hi");
}
}
Now I want different ways to create a main method. Could you explain me?
现在我想要不同的方法来创建一个主要方法。你能给我解释一下吗?
回答by Miral Modi
public static void main(String[] argument)
public static void main(String argument[])
public static void main(String... args)
public static synchronized void main(String... args)
public static strictfp void main(String... args)
public static final void main(String... args)
回答by jonathanasdf
From the Java Documentationthere are only two ways:
从Java 文档中,只有两种方法:
public static void main(String[] args)
and
和
public static void main(String... args)
回答by Ruthreshwar
The multiple ways of declaring the main method is (As everyone explained above)
声明主要方法的多种方式是(正如上面每个人所解释的)
- public static void main(String[] args) or public static void main(String args[])
- public static void main(String... args).The positions of public and static may change as the programmer wish. But remember void should always come before main method. You can also use any parameters for the main method but the main with String[] argswill only be executed first. You can also execute a java program without a main method. For this you need to make use of static block with a break statement at the end.
- public static void main(String[] args) 或 public static void main(String args[])
- 公共静态无效主(字符串...参数)。public 和 static 的位置可以根据程序员的意愿改变。但是记住 void 应该总是在 main 方法之前。您还可以为 main 方法使用任何参数,但带有String[] args的 main只会首先执行。您也可以在没有 main 方法的情况下执行 java 程序。为此,您需要在最后使用带有 break 语句的静态块。
回答by Adithya
Look at the methods below. Which ones will not compile? Which ones will compile, but can't be used as entry points into an application? Which ones compile and act as you would expect a main method to act?
看看下面的方法。哪些不会编译?哪些可以编译,但不能用作应用程序的入口点?哪些像您期望的主要方法一样编译和运行?
if any doubts in this regard please verify the following link
如果对此有任何疑问,请验证以下链接
回答by Freak
There are two possible ways
By using single argument
有两种可能的方法
通过使用单个参数
public static void main(String args) { .. }
Or by varargs
或者通过可变参数
public static void main(String... args) { .. }
Keep in mind that args
in (String args)
is just a argument name.You can use anything here like (String abc)
, (String myargs)
etc.
One last thing is that ,you can also pass a multi dimensional array from main like this
请记住,args
in(String args)
只是一个参数名称。你可以在这里使用任何东西(String abc)
,(String myargs)
等等。
最后一件事是,你也可以像这样从 main 传递一个多维数组
public static void main(String[][] args) { .. }
回答by Murali Prasanth
you can also do this
你也可以这样做
static public void main(String args[])
回答by Bhesh Gurung
You can use var-args instead of array:
您可以使用 var-args 代替数组:
public static void main(String... args) {
回答by Averroes
Also this
还有这个
public static void main(String... args)