Create a Java source file and explain the knowledge involved
PS: You need to have your own judgment and improve your ability in this aspect. You cannot look at the wind to give the rudder. You cannot say this by default. You cannot stick to the rules, this should be proved by your own methods. HelloWorld is the beginning of most articles. Let's enter the Java World. I can't write it like this. I will start with my article questions. First, let's take a look at what is a Java source file. A Java application has one or more extensions: ". "java" file structure, and these files are called Java source files, and from the compilation point of view is called the compilation unit. Its structure can only contain the following content (except for spaces and comments): zero or one package declaration statement. The package declaration statement uses the keyword package, A Java source file can contain only one package statement at most, and is located at the top of the file (comments and spaces are not included). If a package statement is not provided, the class is located in the default package. Of course, the package statement has the following three main functions: first, ---> the class with the same name can be distinguished. For example, there are two classes named Book, one of which represents the Book of the bookstore, and the other represents the user manual of the computer, which can be differentiated by placing it in different packages. Second, ---> it helps implement access permission control. That is to say, when classes in different packages access each other, they will be restricted by access permissions. Third, ---> it is quite good to divide and organize various classes in Java applications. For example, if a well-known learning website has a customer interface, a server side, and a tool class, you can use a package to organize and divide the classes in the corresponding packages. In fact, how to divide the package structure depends on the actual project. Zero or multiple package import statements. The package import Statement uses the keyword import. For Java, You need to import java. if the lang Package is imported by default without the keyword import, otherwise the class from the other package is accessed, the class used must be imported using the import keyword statement. A little bit: the import statement does not cause class initialization, that is to say, if a package contains many classes, it does not mean that the Java Virtual Machine will load all the classes in the package into the memory and initialize them (this will be detailed later ). Zero or multiple classes declare zero or multiple interfaces. Remember that each Java source file can contain multiple classes or interface definitions, however, at most one class or interface can only be public, and the Java source file must be named by the name of the public class. In this case, if the source file name is not a class name, an error occurs during compilation. The order of the package and import statements is fixed. In the Java source file, the package must be in the first line (not included in the comment line), followed by the import Statement, the next step is the declaration of the class or interface. Haha !!! HelloWorld copy code public class HelloWorld {public static void main (String [] args) {System. out. println ("Hello, Welcome to Java !!!");} Although the code above is very simple, it is the entry of the Java application. Every Java application starts to run from the main () method. Public --- access permission; static --- static method; main (String [] args) --- Parameter Restriction; void --- return type. args is only a parameter of the main () method, it is a String-type array, but the parameter name can also be called by other names. In addition, because the static modification method is of the final type by default (cannot be overwritten by the quilt class, therefore, adding final to main () is also feasible. Pass parameters to the main party. When you run a java application using a Java command, you can pass the parameter format to the main method in the command line: java classname [args…], For example, for java Apple parameter1 parameter2, the parameters are parameter1 and parameter2. The length of the args array is 2. If no parameter is passed, the length of the args is 0. You can write a code test, I won't show it here. Annotation statement ---> annotations can be added anywhere in the Java source file, and the Java compiler ignores annotations in the program. Three formats are provided ://... --- The beginning and end of the line are all annotated and will be ignored by the compiler. /*... */--- The comments from "/*" to "*/" are ignored by the compiler. /**... */--- All are comments, which will be ignored by the compiler. If such comments appear before any declaration (such as the declaration of class, class member variables or member methods), they will serve as the content of the JavaDoc document. I won't type the keywords in the Java language one by one. Cut a small image. Create object -- new reference -- this, super method return type -- void each keyword has a special meaning, has its own mission. For example, the class keyword is used for class declaration, and the void keyword is that the method does not return a value. The preceding common keywords will be introduced later. The Java language contains reserved keywords, that is, reserved keywords. Although they are not used as keywords, they may be used in the future. They are const and goto. When using Java keywords, note that all keywords are in lower case. friendly and sizeof are not Java keywords. identifiers in programs cannot be named by keywords. What is the identifier as mentioned above? It refers to the name of the program package, class, interface, variable or method. Java identifiers must comply with the following naming rules: 1. the first letter of the identifiers must be a letter, a hyphen (_), or a dollar sign ($ ". 2. The identifier is composed of digits (0-9), uppercase letters (A-Z), lowercase letters (a-z), underdrawn line "_", and dollar signs. 3. The identifier cannot be a keyword or a reserved word. 4. There is no length limit for the identifier. 5. The identifier is case sensitive. The rules for Identifiers mentioned above also have certain specifications for Java programming. The main points are as follows: first, the class name and interface name-the first letter in upper case. If the class name or interface name is composed of several words, the first letter of each word is capitalized, And the other letters are lowercase. For example, RedApple. Second, the method name and variable name-lower-case letters are also composed of several words, except for the first word, the first letter of each other word is in upper case, and the remaining words are in lower case, such as getInstance, in addition, if the number of objects referred to by the variable name is in the plural form, the variable name must be in the plural form. Again, the package name --- all lowercase letters, such as com. jang. Finally, the constant name is in upper case. If it is composed of multiple words, the words are separated by a line below to facilitate the identification of each word.