简体   繁体   English

`public static void main args` 是什么意思?

[英]What does `public static void main args` mean?

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这里有一点详细解释为什么 main 方法被声明为

public static void main(String[] args)

Main method is the entry point of a Java program for the Java Virtual Machine(JVM). Main 方法是 Java 虚拟机 (JVM) 的 Java 程序的入口点。 Let's say we have a class called Sample假设我们有一个名为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 .该程序将在编译为java Test后执行。 The java command will start the JVM and it will load our Test.java class into the memory. java命令将启动 JVM 并将我们的Test.java类加载到内存中。 As main is the entry point for our program, JVM will search for main method which is declared as public , static and void .由于 main 是我们程序的入口点,JVM 将搜索声明为publicstaticvoid的 main 方法。

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 .为了访问包外的 main ,我们必须将其声明为public If we declare it as anything other than public it shows a Run time Error but not Compilation time error .如果我们将其声明为public以外的任何内容,它会显示Run time Error而不是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();如果一个方法被声明为static方法,那么我们可以在类外部调用该方法,而无需使用语法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() . main()必须声明为 void,因为 JVM 不期望main()有任何值。 So,it must be declared as void .因此,它必须声明为void

If other return type is provided,the it is a RunTimeError ie, NoSuchMethodFoundError .如果提供了其他返回类型,则它是一个RunTimeErrorNoSuchMethodFoundError

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. main( ) 必须将字符串参数作为数组,因为 JVM 通过传递命令行参数来调用 main 方法。 As they are stored in string array object it is passed as an argument to main() .由于它们存储在字符串数组对象中,因此它作为参数传递给main()

According to the Java language specification , a Java program's execution starts from main() method.根据Java 语言规范,Java 程序的执行从 main() 方法开始。 A main() method should follow the specific syntax, it can be explained as: 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. static - 访问修饰符。 A main method should always be static, because the `main()' method can be called without creating an instance of the class. 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()方法设为静态,JVM 调用main()方法而无需先创建对象。

In Java your main method must always be:在 Java 中,您的主要方法必须始终是:

public static void main(String args[])
  1. The program execution starts with main() function, hence the main() function.程序执行从main()函数开始,因此是main()函数。

  2. It must be public so that it is accessible to the outside environment.它必须是public的,以便外部环境可以访问它。

  3. The main() method is always static because, as you know that the program execution starts at main() method and there is no instance of the class containing main() method is initiated. main()方法始终是静态的,因为如您所知,程序执行从main()方法开始,并且没有启动包含main()方法的类的实例。 Hence as the static method can run without need of any instance it is declared of static.因此,由于静态方法可以在不需要任何实例的情况下运行,因此它被声明为静态的。

  4. 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. Java 是独立于平台的,因此您可以尝试在一个系统上编译 java 文件并尝试在另一个系统上执行类文件。 Each machines' bit architecture may be different hence the return type of the main function must always be main() .每台机器的位架构可能不同,因此主函数的返回类型必须始终为main()

Hope this helps.希望这可以帮助。

public --> Access specifier. public --> 访问说明符。 Any other class can access this method.任何其他类都可以访问此方法。

static --> The method is bound to the class, not to an instance of the class. static --> 方法绑定到类,而不是类的实例。

void --> Return type. void --> 返回类型。 The method doesn't return anything.该方法不返回任何内容。

main(String[] args) --> method name is main() . main(String[] args) --> 方法名是main() It takes an array of String's as argument.它需要一个字符串数组作为参数。 The String[] args are command line arguments. String[]参数是命令行参数。

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()方法是程序的入口点,如果您更改签名,那么您的程序可能无法运行。

  • Public = This method is visible to all other classes. Public = 此方法对所有其他类可见。

  • static = This method doesn't need an instance to be ran. static = 此方法不需要运行实例。

  • void = This method doesn't return anything. void = 此方法不返回任何内容。

  • main() = Main method (First method to run). main() = Main 方法(第一个运行的方法)。

  • String[] = Array of strings. String[] = 字符串数组。

  • args = Array name. args = 数组名称。

请通过此视频链接 -> https://youtu.be/ggsRGcA8hnQVery获得对 public static void main(String args[]) 方法的清晰和中肯的解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是`public static <T> void main(String [] args)`代表什么? - What does `public static <T> void main(String[] args)` stand for? “public static void”在Java中意味着什么? - What does 'public static void' mean in Java? “public static void main(String args[])”的Eclipse快捷方式是什么? - What is the Eclipse shortcut for “public static void main(String args[])”? 了解公共静态void main(String [] args) - Understanding public static void main(String[] args) 对main方法的误解/“ public static void main(String [] args)”的观点 - Misunderstanding main method/the point of “public static void main(String[] args)” public static void main(String args[]) 为什么 string args[] 是强制性的,为什么我们不使用 public static void main(Object args[])? - public static void main(String args[]) why string args[] is compulsory ,why we not use public static void main(Object args[])? 我的带有简单println的java public static void main(String [] args)在android studio中不起作用 - my java public static void main(String []args) with a simple println does not work in android studio 是否所有要在 java 程序中运行的代码都需要公开 static void main(String[] args)? - Does all code to be run in a java program need to be in public static void main(String[] args)? 需要添加“ public static void main(String [] args)”, - Need to add a “public static void main(String[] args)”, public static void main(String[] args) 抛出异常 - public static void main(String[] args) throws Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM