Do you know the public ry for public static void main (string ARGs?

Source: Internet
Author: User

Notes on the main () method and command line parameters in Java

 

Complete definition syntax of the main method: public static void main (string [] ARGs ){}

Command line format: Java [command parameters] file name [ARGs [0] (USER Parameters) ARGs [1]... for example, Java-CS Aclass a B C <enter>


1. the modifier public in the main () method
The Java virtual machine specification does not require the main to be public, so public can be omitted. It is only for the main () method to be called from any Java Runtime Environment. However, though there is no public, the compilation is normal, but the virtual machine cannot start the application.


2. About the keyword static in the main () method
The static method cannot access instance fields. However, you can access static fields in your own class.

The main method must be a static method, so the static method must be written. To avoid the need to construct a class instance. If there is no static, an error occurs during running.


3. Return type and return value of main () method
Main (string [] Arg) Compilation error if void is not written
If neither static nor [] is specified, a running error occurs.

The main () method does not return "Exit code" to the operating system ". In Java, if the main method Exits normally, the exit code of the Java program is 0, indicating that the program is successfully implemented. If you want to terminate the program with different exit codes, use the system. Exit method.

Because the main () method can be reloaded, the return type can be void, Int, Boolean... however, if you look at the main () method of the application entry, you can only use void to return.


4. Notes related to the main () method and command line parameters

When the string [] ARGs is missing, although the compilation is successful, the main method cannot be obtained during the runtime. An error is reported during the runtime. The method parameter name ARGs can be changed.

To know the length of the string array, you can use args. length directly.

If ARGs [0] is accessed without user parameters, an array out-of-bounds exception is thrown.

The following operations: Because the parameter type of the main () method is correct, compilation can be successful, but an error occurs during running.

Public class test
{
Public static void main (string argv)
{
System. Out. println ("Hello Cruel world ");
}
}

If the access to args [] is exceeded, the compilation is normal and an error occurs.
For example:
Public class test {
Public static void main (string [] ARGs ){
String Foo = ARGs [1];
String bar = ARGs [2];
String Baz = ARGs [3];
}
}
Java test red green blue
What is the value of Baz?
A. Baz has value ""
B. Baz has value of null
C. Baz has value of "red"
D. Baz has value of "blue"
E. Baz has value of "green"
F. The Code does not compile
G. The program throw an exception
Answer: G

5. The main () method serves only as the entry to the Java application. The main () method is not required for Applet programs. Generally, the main () method is started in different ways in the browser, and the entry program is usually the init () method.


6. Main can be used as the common method name.
For example:
Public class Droitwich
{
Class One
{
Private Class Two
{
Public void main () // note the main method
{
System. Out. println ("two ");
}
}
}
}
Show: compilation is normal

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.