“public static void main args"是什么意思? [英] What does `public static void main args` mean?
问题描述
我不知道这是什么意思,每当你写代码之前,人们都会这么说
public static void main(String[] args) {
这是什么意思?
这里稍微详细解释一下为什么main方法被声明为
public static void main(String[] args)
Main 方法是 Java 虚拟机 (JVM) 的 Java 程序的入口点.假设我们有一个名为 Sample
class 示例 {静态无效乐趣(){System.out.println("你好");}}类测试{public static void main(String[] args){示例.fun();}}
这个程序会在编译后作为java Test
执行.java
命令将启动 JVM 并将我们的 Test.java
类加载到内存中.由于 main 是我们程序的入口点,JVM 将搜索声明为 public
、static
和 void
的 main 方法.
为什么必须将 main 声明为 public?
main()
必须声明为 public
因为我们知道它在程序执行开始时被 JVM 调用,并且 JVM 不属于我们的程序包.>
为了访问包外的 main,我们必须将其声明为 public
.如果我们将其声明为 public
以外的任何内容,它会显示 Run time Error
而不是 Compilation time error
.
为什么必须将 main 声明为静态?
main()
必须声明为静态,因为 JVM 不知道如何创建一个类的对象,所以它需要一种标准的方式来访问 main 方法,这可以通过声明 main()
作为 static
.
如果一个方法被声明为static
,那么我们可以在类之外调用该方法而无需使用语法ClassName.methodName();
创建一个对象.
这样JVM就可以将我们的main方法调用为
为什么必须将 main 声明为 void?
main()
必须声明为 void,因为 JVM 不期望来自 main()
的任何值.所以,它必须声明为void
.
如果提供了其他返回类型,则为 RunTimeError
,即 NoSuchMethodFoundError
.
为什么 main 必须有字符串数组参数?
main(
) 必须有 String 参数作为数组,因为 JVM 通过传递命令行参数来调用 main 方法.由于它们存储在字符串数组对象中,因此它作为参数传递给 main()
.
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?
Here is a little bit detailed explanation on why main method is declared as
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
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
.
Why main must be declared 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.
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
.
Why main must be declared static?
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
.
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();
.
So in this way JVM can call our main method as <ClassName>.<Main-Method>
Why main must be declared void?
main()
must be declared void because JVM is not expecting any value from main()
. So,it must be declared as void
.
If other return type is provided,the it is a RunTimeError
i.e., NoSuchMethodFoundError
.
Why main must have String Array Arguments?
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()
.
这篇关于“public static void main args"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!