Public static void main (string [] args) explanation, staticargs

Source: Internet
Author: User

Public static void main (string [] args) explanation, staticargs

This problem has been bothering me for a long time. I will check it out today. After all, my curiosity is heavy.

 

1. why "public"

Because Java programs are called through jvm virtual machines, if the main () function is to be called, it must be public

 

2. why "static"

In java, there are no static variables or functions. To be called, you must create an object first. The main function, as the entry to the program, needs to be started before other functions are instantiated, which is why a static function is added. The main function is like a door. To explore other functions, you must first enter the program from the door. Static provides such a feature that can be started without the need to create an object.

 

3. why "(String [] args )"?

(1) Let's talk about why this string array is called "args". In fact, this is a conventional name. It is also OK if it is not called "args, if you do not believe it, you can test it on your own.

(2) Let's take a look at why it is a String array. java is running like this (below), a bit like command line, a1, a2, a3 is the command in the String array.

java main a1 a2 a3 

 

 

Next we will go deep into the jvm driver to see how the main () function is started:

mainID = (*env)->GetStaticMethodID(env, mainClass, "main", "([Ljava/lang/String;)V");  

  

 

 

This is the code for jvm startup. Go directly to static and set the parameter to the main method of the String array.

 

 

Author's Public Account: Human Machine Learning

I have been sharing machine learning practices for a long time. Thank you for your attention!

 

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.