What does the instance analysis of public static void main (String args[]) in Java mean _java

Source: Internet
Author: User

This example describes the ins and outs of public static void main (String args[] in Java. Share to everyone for your reference, specific as follows:

public static void Main (string[] args)

This is definitely not a fantasy, not unreasonable death rules, but the need for Java program implementation.

The JVM first checks to see if the class contains a special method before attempting to run a class. This method must be public so that it can be accessed at any location. This method must be static, because the method cannot rely on any instance of the class to run, rather than a static method, and the instance object of the class is created before it is run.

This method has no return value. Unlike C + + programs, Java programs default to the normal end, so main does not return int. You can use System.exit (1) If you want to end the program in a non normal state.

This method must accept an indefinite number of string parameters, because the runtime may want to attach a running parameter. Like the Java HelloWorld Jack 100, here's Jack and 100 are running parameters.

Why is it a string? Because string is universal. Any literal form can be interpreted as string, and other types are not (such as Jack cannot be interpreted as an integer or floating-point number), so it is most appropriate to use string to store parameters. And because the number of parameters is not limited to one, so the array, that is, string[]. After Java 1.5, you can also write a string ... to indicate a variable number.

As for the parameter variable name can be arbitrary, as long as the guarantee within the method of the variable name to get the parameters of the line, from the role of variables, called arguments or args is certainly the most appropriate.

As for the name of this method must be main, there are historical reasons, because the first C with the main function as a program portal, Java followed this historical tradition.

Let's take an example to explore:

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 modifiers, maximum permissions.
  static: Loading with the Miandemo class, disappearing and disappearing.
  void: No return value
  main: function name, JVM recognized special function name
  (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 ();
  }
  /*
  Summary:
  1, the main () function is called by the JVM, the JVM to the main () passed a length of 0 of the string array, that is, new String[0]
  2, you can pass the class name. Main () to perform a value operation, because main The () method is static.
  */
}

Run Result:

I hope this article will help you with Java programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.