Example analysis of what does public static void main(String args[]) mean in Java

This article is well written and reprinted! You can first look at Baidu's answer:

This is the entry address of the java program. When the java virtual machine runs the program, the first thing it looks for is the main method. The function of the main() function in the C language is the same. Only the java program with the main() method can be used by java. The virtual machine is ready to run, which can be understood as the specified format 

for the parameters and modifiers
in it. The access rights of the program represented by public: indicate that it can be referenced in any occasion, so that the java virtual machine can find the main() method. To run the javac program

static: indicates that the method is static, does not depend on the object of the class, and belongs to the class. When the class is loaded, the main() method is also loaded into the memory with the

void: main() method is not The main that needs to return the value
: by convention, the specified
String[] args: receive parameters from the console





Example analysis of what does public static void main(String args[]) mean in Java

Author: NW_KNIFE Font: [ Increase and  decrease ] Type: Reprint time: 2015-12-09  I want to comment

This article mainly introduces the meaning of public static void main(String args[]) in Java by example analysis, and analyzes the specific meaning and usage of the main keyword declaration of Java main function in detail. Friends who need it can refer to the following

The example of this article describes the ins and outs of public static void main(String args[]) in Java. Share for your reference, the details are as follows:

public static void main(String[] args)

This is definitely not out of thin air, nor is it an unreasonable dead rule, but a need for java program execution.

Before the jvm tries to run a class, it checks whether the class contains a special method. This method must be public so that it can be accessed anywhere. This method must be static, because this method cannot rely on any instance of the class to run, and non-static methods must create an instance of the class before running.

This method has no return value. Unlike C/C++ programs, java programs end in normal by default, so main does not return int. If you want to end the program abnormally, you can use System.exit(1).

This method must accept a variable number of String type parameters, because the operator may have to additionally run parameters. Such as java HelloWorld jack 100, where jack and 100 are running parameters.

Why String? Because String is universal. Any literal form can be interpreted as a String, but other types are not (such as jack cannot be interpreted as an integer or floating point number), so String is the most suitable for storing parameters. And because the number of parameters is not limited to one, an array is used, namely String[]. After Java 1.5, it can also be written as String..., indicating an indeterminate number.

As for the variable name of the parameter, it can be arbitrary, as long as the parameter is obtained according to the variable name inside the method. From the perspective of the function of the variable, it is of course the most appropriate to call it arguments or args.

As for why the name of this method must be main, there are historical reasons, because the earliest C used the main function as the program entry, and java followed this historical tradition.

Let's take a look at an example:

package test;
public class MainTest {
  public static void main(String[] args)
  {
    for (int x=0; x<args.length; x++)
    {
      System.out.println(args[x]);
    }
  }
  public static void sysInfo(){
    System.out.println("call sysInfo()");
  }
}
public class MainDemo {
  /*
  public static void main(String[] args)
  public:权限修饰符,权限最大。
  static:随着MianDemo类的加载而加载,消失而消失。
  void: 没有返回值
  main: 函数名,jvm识别的特殊函数名
  (String[] args):定义了一个字符串数组参数
  */
  public static void main(String... args) 
  {
    String[] arr = new String[]{"zhang","li","wang","zhao"};
    MainTest.main(arr);
    MainTest.sysInfo();
  }
  /*
  总结:
  1、main()函数是被jvm调用,jvm给main()传了一个长度为0的字符串的数组,也就是new String[0]
  2、可以通过类名.main()进行传值操作,因为main()方法是static的。
  */
}

运行结果:

希望本文所述对大家Java程序设计有所帮助


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325797688&siteId=291194637