Re-recognize the components of the main function in Java

Source: Internet
Author: User

The main function of each component in-depth understanding
<span style= "White-space:pre" ></span>public static void Main (string[] agrs)


Main function: is a special function, as the entry of the program, can be called by the JVM


the definition of the main function:
Public: Represents the maximum access permission for the function
Static: Represents the main function with the loading of the class already exists
void: The main function does not have a specific return value
Main: Not a keyword, but a special word that can be identified by the JVM
(string[] args): The parameter of the function, the parameter type is an array of elements in the array, the string array. The length of this spatiotemporal array of main (string[] args) string array is 0, but can also be The parameter is passed to it when it is run.

The main function is fixed-format, the JVM recognizes

The main function can be overloaded, but the JVM only recognizes main (string[] args), and the others are general functions. The args knowledge array variable in this can be changed, and the other cannot be changed. A Java file can contain many classes, each of which has only one main function, but each Java file can contain more than one main function, at run time, you need to specify which JVM entry is. When a main function such as a class can call the main function of another class.


Case one: A Java file can contain multiple classes, each of which can contain only one main function. When a Java file contains multiple main functions, the program entry is specified.

<pre name= "code" class= "java" >class maindemo {public static void main (string[] args) {System.out.println ( Args.length)//system.out.println (args[1]);//This is the wrong System.out.println ("maindemo----0"); main (1);} public static void main (int x) {SYSTEM.OUT.PRINTLN ("Overloaded main function---" +x);}} Class MainDemo1 {public static void main (string[] args) {System.out.println (args.length);//system.out.println (Args[1] );//This is the wrong System.out.println ("Maindemo----1");}}


Case TWO: The program entry main function at execution time, you can pass to the same parameters as normal functions.

Class Maindemo{public static void Main (string[] args) {string[] arr={"haha", "hha", "Heihei", "Xixi", "Hiahia"}; Maintest.main (arr);}} Class Maintest{public static void Main (string[] args) {for (int x=0;x<args.length;x++) {System.out.println (args[x]);}}}





Re-recognize the components of the main function in Java

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.