Java “public static void main args”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29276917/
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
What does `public static void main args` mean?
提问by Nick Nicholas
I am not sure what this means, whenever before you write a code, people say this
我不确定这是什么意思,每当你写代码之前,人们都会这样说
public static void main(String[] args) {
What does that mean?
这意味着什么?
回答by Vivek Aditya
public
--> Access specifier. Any other class can access this method.
public
--> 访问说明符。任何其他类都可以访问此方法。
static
--> The method is bound to the class, not to an instance of the class.
static
--> 方法绑定到类,而不是类的实例。
void
--> Return type. The method doesn't return anything.
void
--> 返回类型。该方法不返回任何内容。
main(String[] args)
--> method name is main()
. It takes an array of String's as argument. The String[]
args are command line arguments.
main(String[] args)
--> 方法名称是main()
. 它需要一个字符串数组作为参数。该String[]
ARG游戏的命令行参数。
Note: The main()
method defined above is the entry point of a program, if you change the signature, then your program might not run.
注意:main()
上面定义的方法是程序的入口点,如果更改签名,则程序可能无法运行。
回答by Frunk
Public
= This method is visible to all other classes.static
= This method doesn't need an instance to be ran.void
= This method doesn't return anything.main()
= Main method (First method to run).String[]
= Array of strings.args
= Array name.
Public
= 此方法对所有其他类可见。static
= 此方法不需要运行实例。void
= 此方法不返回任何内容。main()
= Main 方法(要运行的第一个方法)。String[]
= 字符串数组。args
= 数组名称。
回答by Giri
According to the Java language specification, a Java program's execution starts from main() method. A main() method should follow the specific syntax, it can be explained as:
根据Java 语言规范,Java 程序的执行从 main() 方法开始。main() 方法应该遵循特定的语法,可以解释为:
public static void main(String[] args)
public
- Access specifier, shows that main()
is accessible to all other classes.
public
- 访问说明符,显示main()
所有其他类都可以访问。
void
- return type, main()
returns nothing.
void
- 返回类型,不main()
返回任何内容。
String args[]
- arguments to the main()
method, which should an array of type string.
String args[]
-main()
方法的参数,它应该是一个字符串类型的数组。
static
- Access modifier. A main method should always be static, because the `main()' method can be called without creating an instance of the class.
static
- 访问修饰符。main 方法应该始终是静态的,因为可以在不创建类的实例的情况下调用 `main()' 方法。
Let us assume, we are executing a Helloworld java program.
让我们假设,我们正在执行一个 Helloworld Java 程序。
While executing the program, we use the command
在执行程序时,我们使用命令
java Helloworld.
Internally, this command is converted into
在内部,这个命令被转换成
Helloworld.main()
By making main()
method static, JVM calls the main()
method without creating an object first.
通过将main()
方法设为静态,JVMmain()
无需先创建对象即可调用该方法。
回答by Sai Upadhyayula
Here is a little bit detailed explanation on why main method is declared as
这里有一些关于为什么 main 方法被声明为的详细解释
public static void main(String[] args)
Main method is the entry point of a Java program for the Java Virtual Machine(JVM). Let's say we have a class called Sample
Main 方法是 Java 虚拟机 (JVM) 的 Java 程序的入口点。假设我们有一个类叫做Sample
class Sample {
static void fun()
{
System.out.println("Hello");
}
}
class Test {
public static void main(String[] args)
{
Sample.fun();
}
}
This program will be executed after compilation as java Test
. The java
command will start the JVM and it will load our Test.java
class into the memory. As main is the entry point for our program, JVM will search for main method which is declared as public
, static
and void
.
该程序将在编译为java Test
. 该java
命令将启动 JVM,并将我们的Test.java
类加载到内存中。由于 main 是我们程序的入口点,JVM 将搜索声明为public
,static
和 的main 方法void
。
Why main must be declared public?
为什么必须将 main 声明为 public?
main()
must be declared public
because as we know it is invoked by JVM whenever the program execution starts and JVM does not belong to our program package.
main()
必须声明,public
因为我们知道它在程序执行开始时由 JVM 调用,并且 JVM 不属于我们的程序包。
In order to access main outside the package we have to declare it as public
. If we declare it as anything other than public
it shows a Run time Error
but not Compilation time error
.
为了访问包外的 main 我们必须将其声明为public
. 如果我们将其声明为任何其他内容,public
则显示 a Run time Error
but not Compilation time error
。
Why main must be declared static?
为什么 main 必须声明为静态?
main()
must be declared as static because JVM does not know how to create an object of a class, so it needs a standard way to access the main method which is possible by declaring main()
as static
.
main()
必须声明为静态,因为 JVM 不知道如何创建类的对象,因此它需要一种标准方法来访问 main 方法,这可以通过声明main()
为static
.
If a method is declared as static
then we can call that method outside the class without creating an object using the syntax ClassName.methodName();
.
如果方法声明为 asstatic
那么我们可以在类外调用该方法,而无需使用语法创建对象ClassName.methodName();
。
So in this way JVM can call our main method as <ClassName>.<Main-Method>
所以通过这种方式 JVM 可以调用我们的 main 方法作为 <ClassName>.<Main-Method>
Why main must be declared void?
为什么 main 必须声明为无效?
main()
must be declared void because JVM is not expecting any value from main()
. So,it must be declared as void
.
main()
必须声明为 void,因为 JVM 不期望来自main()
. 所以,它必须声明为void
。
If other return type is provided,the it is a RunTimeError
i.e., NoSuchMethodFoundError
.
如果提供了其他返回类型,则为RunTimeError
ie, NoSuchMethodFoundError
。
Why main must have String Array Arguments?
为什么 main 必须有字符串数组参数?
main(
) must have String arguments as arrays because JVM calls main method by passing command line argument. As they are stored in string array object it is passed as an argument to main()
.
main(
) 必须将字符串参数作为数组,因为 JVM 通过传递命令行参数来调用 main 方法。由于它们存储在字符串数组对象中,因此它作为参数传递给main()
.
回答by vineeth
In Java your main method must always be:
在 Java 中,您的 main 方法必须始终是:
public static void main(String args[])
The program execution starts with
main()
function, hence themain()
function.It must be
public
so that it is accessible to the outside environment.The
main()
method is always static because, as you know that the program execution starts atmain()
method and there is no instance of the class containingmain()
method is initiated. Hence as the static method can run without need of any instance it is declared of static.Java is platform independent, hence you may try to compile the java file on one system and try to execute the class file on another. Each machines' bit architecture may be different hence the return type of the main function must always be
main()
.
程序执行从
main()
函数开始,因此是main()
函数。它必须是
public
外部环境可以访问的。该
main()
方法始终是静态的,因为正如您所知,程序执行从main()
方法开始,并且没有main()
启动包含方法的类的实例。因此,由于静态方法可以在不需要任何实例的情况下运行,因此它被声明为静态。Java 是独立于平台的,因此您可以尝试在一个系统上编译 java 文件并尝试在另一个系统上执行类文件。每台机器的位架构可能不同,因此主函数的返回类型必须始终为
main()
.
Hope this helps.
希望这可以帮助。