Home>

public static void main (string [] args)

This is definitely not conceived out of thin air,It ’s not unreasonable death rules,But the need for java program execution.

jvm before trying to run a class,First check if the class contains a special method.This method must be public,So that it can be accessed from anywhere.This method must be static, because this method cannot run on any instance of the class,Instead of static methods, you must create an instance of the class before running.

This method has no return value.Unlike c/c++ programs,Java programs end by default,So main does not return int. If i want to end the program in an abnormal state,You can use system.exit (1).

This method must be able to accept an unlimited number of arguments of type string.Because the operator may need to add operating parameters.Such as java helloworld jack 100, where jack and 100 are the operating parameters.

Why string?Because strings are 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 it is most appropriate to use a string to store parameters.And because the number of parameters is not limited to one,So using an array,That is string []. After java 1.5, it can also be written as string ..., indicating that the number is uncertain.

As for the variable name of the parameter,Just make sure to get the parameter by the variable name inside the method,From the perspective of the role of variables,Calling arguments or args is of course the most appropriate.

As for why the name of this method must be main, for historical reasons,Because the earliest c used the main function as the program entry,Java follows this historical tradition.

Let's take a look at it with 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:permission modifier,The highest authority.
  static:Loaded as the miandemo class is loaded,Disappear and disappear.
  void:No return value
  main:function name, special function name recognized by jvm
  (string [] args):defines a string array parameter
  * /
  public static void main (string ... args)
  {
    string [] arr=new string [] {"zhang", "li", "wang", "zhao"};
    maintest.main (arr);
    maintest.sysinfo ();
  }
  /*
  to sum up:
  1. The main () function is called by the jvm. The jvm passes an array of strings of length 0 to the main ().
Which is new string [0]
  2.You can pass the class name.
main () performs the value passing operation,Because the main () method is static.
  * /
}

operation result:

  • Previous A complete example of the progress bar effect displayed at the top of the page with jQuery
  • Next JavaScript observer mode (classic)