Java thousands ask _03 basic syntax (016) _main method is what

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What is the Main method?

There is a main () method in a class, which means that this is a Java application that can start the running program directly (the JDK or JRE is installed in the operating system). Any non-abstract class/interface can add the main () method.

Learn about abstract classes, interfaces see here: What is the difference between an interface and an abstract class?
In the Java specification, the Declaration of the main () method is:

publicstaticvoidmain(String args[]){}

When a class is run by the Java Run Tool: the Java class name, the JRE runs the main () method in the class. When you run this Java application, the main method is called first, because the main method is public static, so it is called without instantiating the object of the class, but directly through the class name.

Understand public, private look here: public, privite, protected what is the difference
Here are a few things to note about main:

    1. For the main method in Java, the main method cannot have a return value because the return value type of main in the specification is void.

    2. The input parameter of the main method, type string[], the specification of the main () method must have a parameter string[], of course, the name of the parameter can be set by itself. According to Custom, the name of the string array is generally consistent with the Mian parameter name in the canonical example, named Args.

    3. The parameter args of the main (string[] args) method can be specified at run time, for example: Java Testmain 1 2 3, and args is [all-in-all].

    4. The main method can throw an exception through the throws exception declaration, and if an exception occurs, it is thrown directly into the run tool (the run tool typically prints the error log to the console).

    5. Similar to the normal class with the main method, the static code block static{} of the class is executed before main execution.

One example of this is the combination of the following:

 Public classTestmain {Static{System. out. println ("Hello wordld"); } Public Static void Main(string[] args) throws Exception {if(Args.length >0) { for(String Arg:args) {System. out. println ("args:"+ arg); }        }if(Args.length <=0) {Throw NewException ("Exception"); }    }}

The results of the Java Testmain implementation are as follows:

Hello WORDLD
Exception in thread "main" java.lang.Exception:Exception
At Com.test.TestMain.main (testmain.java:16)

The results of "Java Testmain 1 2 3" are as follows:

Hello WORDLD
Args:1
Args:2
Args:3

Java thousands ask _03 basic syntax (016) _main method is what

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.