Java-Preliminary Understanding-seventh Chapter-this key word main function parsing

Source: Internet
Author: User

One.

For main function public static void main (string[] args):

The main function is a fixed-notation function, which is also a general function, but where is it special?

Fixed format Why do you write this?

Public: Because the permissions must be the largest, small permissions, bad for the virtual machine, if written to private, how to access?

Static: When the virtual machine calls the main function, it does not need the object, directly with the main function belongs to the class name.

Java in Java Staticdemo is when you start the virtual machine, and then you start to tell the class to run. The virtual machine takes the class name directly. Main (aka Staticdemo.main), call the appropriate method. Would you like to have new Staticdemo this object (this means that the method was called by the object, or called by the Class)? No, if the new object, it will be troublesome. The new object means that a lot is produced. Which object is called, does it have special data? I don't know. Calling directly with the class name is done.

void: The main function does not have a specific return value.

Why? return who? Do you have a definite direction? is not returned to the caller, there is a return direction, who calls back to whom. If you really want to return it, return it to the Java virtual machine. It is necessary to return, and the end is necessary. What is the use of giving a virtual machine an integer type value? The virtual machine does not need to return a value, and the entrance is finished.

Mian: is the function name, not the keyword, just a fixed name that the JVM recognizes.

String[] args: This is the argument list of the main function, is an array type parameter, and the element is a string type.

Two.

Any function is called, and if it has parameters, it is passed the corresponding argument. What is the passed argument? Who is calling the main function? The virtual machine is calling the main function. The principle is that if there are arguments, the corresponding arguments must be passed, and if not passed, the equivalent of not calling the function, then what is the actual argument passed? If you do not know what is being passed, if you want to know, then how to verify. Args is a variable, print directly, see the output will know.

The result of the above DOS shows that the @c17164 is the long,string value, the left [is the array, L is the string type, and that is, the data stored in this array is the type of the strings. We can see this, what does that mean? Indicates that the virtual machine did pass a string-type array entity to the main method when it called the main function. This is a must, why? Because if there is no entity, the print result is empty. There are entities how to see, c17164 is the address, there is the address of the West is right, it proves that there are entities. So, it should be an array entity object that was created. Closely followed, the tangle of our problems is not here. Did you create an array of string types, new string[], and what is the length? When an array is established, it must have a length, which is depressing, what is the length? Printed by args.length, the result shows a length of 0. It can be concluded that the virtual machine in the call to the main function, it is in a new string[0], the final virtual machine is passed in a parameter, it does not pass the element inside, because the length is 0.

It does not seem to make sense to look at the arguments entered by this JVM. The main function with such a parameter, because we run the Java program, we can run the program at the same time, specify some specific parameter values, so at the entrance of the program, they left you a room, you put some of the data you need to run the program through the portal, transfer into, You can pass your customizations, which is why the main function left parameters. Why are there so many types of data to leave this type? Cause: 1. If you're going to preach a parameter, what you preach, whether you want to pass an integer, a floating-point number, or, ultimately, a string, and we can convert the string to other types, the string type is the most common data. Explain, if you are on the Internet, there is a text box, write please enter your age, you enter a 24, make sure that you can not marry, lose 24, click a whole, you can get married. There is no, you can get married. This 24 what you enter into is actually a string, that is not an integer, the text box is all text, it only turns the text into a number is the number, so the string is the most common data. If I want to pass a string, what if I want to pass more than one? Never mind, an array. The array is more than one, which solves the problem.

Can you do that? Operate it. Enter Args[0], which is the 0-point standard element that accesses the args array. The DOS results are shown as follows, because the length is 0, there is no 0-point mark, so the display is the corner mark out of bounds.

Now we're not going to let the corners go out of bounds, notice that the compilation is not compiled, and when is the value passed? is not to say that the call function when the value, Java Maindemo, I start a Java virtual machine, tell run this class, it is not holding me this class, call the main function, in this pass value. When the virtual machine calls the main function, it will pass directly to the value, how to pass? After the class name to knock a space, this piece can write things, such as "haha", followed by "hehe", "Xixi", a return to the car,

The results show that there are three elements and the first element is haha. The word wants to divide, must add the space. When a virtual machine sees the contents of a class name, it separates the contents in a space, acts as an element in the array, encapsulates it, and then passes the array to args, where the array is no longer 0, is 3, and takes the element content.

Look closely at the above, the output statement in the For statement position in the back, what is the attention? What if the FOR statement and output statements are written in the same column ?

And sometimes there seems to be a lack of a compilation, namely Javac, below, only the Java command, not the Javac command.

This is called assigning a value to the main function, and I can specify the data I need when I use an application, but how much does it take? Not many, because these unknown data (in the video like to determine the input data is known as unknown data) often users will not use, because you can tell the user, do a backstage, write a Java, write a class name, the unknown data written on, the user will not use, so what do we do? We made a graphical interface, leaving a few Windows boxes, please enter here, this is the length of things, it is very comfortable to do.

The main function is basically finished.

Three. Small details

The parameters in the main function have been modified to turn args into X, is it OK for the line to pass?

Although the main function is a fixed format, but the formal parameters in parentheses, args is the variable name, variables love to write what to write, the only thing that can change is the args.

But have never seen it change, the earliest period, is written arguments (parameters), in order to facilitate the writing, shorthand into args.

Some changes have been made, and the parentheses in main write int x, can it exist? Looks like two entrances, not really, the main function is a fixed format, so the following main (int x) is not a main function. In this case, the method name is the same as the overloaded form of the parameter list. If you write public static void main (string[] x) This makes a mistake, and it really becomes two entrances.

Java-Preliminary Understanding-seventh Chapter-this key word main function parsing

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.