`public static void main args'是什么意思? [英] What does `public static void main args` mean?

查看:117
本文介绍了`public static void main args'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这意味着什么,在你编写代码之前,人们会说这个

I am not sure what this means, whenever before you write a code, people say this

public static void main(String[] args) {

这是什么意思?

推荐答案

这里有一个关于为什么main方法被声明为

Here is a little bit detailed explanation on why main method is declared as

public static void main(String[] args)

主要方法是a的入口点Java虚拟机(JVM)的Java程序。假设我们有一个名为的样本

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();
      }  
}

此程序将在编译后执行 java Test java 命令将启动JVM,它将把我们的 Test.java 类加载到内存中。由于main是我们程序的入口点,JVM将搜索main方法,该方法声明为 public static void

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.


为什么main必须公开?

main()必须声明 public 因为我们知道只要程序执行开始并且JVM不属于我们的程序包,JVM就会调用它。

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.

为了访问在包外面,我们必须将其声明为 public 。如果我们将其声明为 public 以外的任何内容,则会显示运行时错误但不会编译时间错误

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声明为静态?

main()必须声明为静态,因为JVM不知道如何创建对象一个类,所以它需要一种标准的方法来访问main方法,这可以通过将 main()声明为 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.

如果方法被声明为 static ,那么我们可以在类之外调用该方法而不创建对象使用语法 ClassName.methodName();

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();.

因此,这样JVM可以将我们的main方法称为< ClassName>。< Main-Method>

So in this way JVM can call our main method as <ClassName>.<Main-Method>


为何主要必须宣布无效?

main()必须是声明无效,因为JVM是否定的期待 main()中的任何值。因此,它必须声明为 void

main() must be declared void because JVM is not expecting any value from main(). So,it must be declared as void.

如果提供其他返回类型,则为 RunTimeError ie, NoSuchMethodFoundError

If other return type is provided,the it is a RunTimeError i.e., NoSuchMethodFoundError.


为什么main必须有字符串数组参数?

main()必须将String参数作为数组,因为JVM通过传递命令行参数来调用main方法。因为它们存储在字符串数组对象中,所以它作为参数传递给 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().

这篇关于`public static void main args'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆