Java Programming Development: A detailed description of the main () method in Java

Source: Internet
Author: User

This article source: http://www.zretc.com/technologyDetail/346.html

In Java, the main () method is the entry method for a Java application, that is, when the program is running, the first method to execute is the main () method, which differs greatly from other methods, such as the name of the method must be main, and the method must be public For a static void type, the method must receive a string array of arguments, and so on.

Before looking at the main () method in Java, let's look at one of the simplest Java application HelloWorld, which I'll use to illustrate the mystery of the main () method in the Java class, and the code for the program is as follows:

public class HelloWorld {public static void main (String args[]) {System.out.println ("Hello world!");}}

  First, say the class:

The HelloWorld class has the main () method, which indicates that this is a Java application that starts a running program directly from the JVM (Java Virtual machine).

Since it is a class, Java allows classes to be non-public keyword constraints, although the definition of a class can only be restricted to public or unrestricted keywords (default).

  Second, the main () method

The declaration of the main () method is: public staticvoid main (String args[]). This must be defined, which is the Java specification.

Why this is defined is related to the operation of the JVM.

When there is a main () method in a class, executing the command "Java class name" will start the virtual machine to execute the main method in that class.

Since the JVM is running this Java application, it first calls the main method, which does not instantiate the object of the class, but is called directly by the class name and therefore needs to be limited to publicstatic.

For the main method in Java, the JVM has a limit and cannot have a return value, so the return value type is void.

The main method also has an input parameter, type string[], this is the Java specification, the main () method must have a parameter, the class must be string[], as for the name of the string array, this can be set by itself, according to the habit, The name of this string array is generally consistent with the Mian parameter name in the Sun Java specification paradigm, named args.

Therefore, the main () method definition must be: "public static Voidmain (string[] args)"

Java Programming Development: A detailed description of the main () method 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.