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!