Create a Java source file and explain the knowledge involved

Source: Internet
Author: User
Tags uppercase letter java keywords

  PS: To have their own judgment, to improve their ability to this aspect, can not keep a lookout to make the rudder, not everyone said that the default is that it is such a drop, can not be a rut, to pass their own way to prove that this is what should be insisted on.

Most of the articles start HelloWorld, let's go into the Java world. I'm not going to write it like this. I began to start slowly from the topic of my article. To learn what Java source files are, Java applications consist of one or more files with the extension ". Java", which are referred to as Java source files, and are called compilation units from the point of compilation. The structure can contain only the contents (except for spaces and annotations):

0 or a Package declaration statement, the package declaration statement uses the keyword packages, a Java source file can have a maximum of one package statement and at the top of the file (comments and spaces do not count), and no package statement is provided to indicate that the class is in the default packet.

Of course, it's going to happen. This statement is mainly embodied in the following three major functions:

One is---> can distinguish between classes with the same name. For example: There are two classes named book, one for the bookstore, and the other for the computer's instruction manual, which can be distinguished by placing it in a different package at that time.

Second,---> Helps enforce access control. This means that classes that are located between different packages are constrained by access permissions when they are accessed by each other.

The third is---> fairly well able to divide and organize various classes in Java applications. For example: A well-known learning site, has a customer interface, a server-side, a tool class, then you can use the package to organize, the corresponding classes are placed in their respective packages. In fact, how to divide the structure of a package depends on the actual project.

0 or more Package import statements, the package import statement uses the keyword import, For Java, if you need to import the Java.lang package, the default import does not require the import of the keyword, otherwise it accesses classes from another package, and the class that is used must introduce the used class through the IMPORT keyword statement. Little knowledge: The import statement does not cause the initialization of the class, that is, if there are many classes in a package all introduced does not mean that the Java virtual opportunity to load all the classes in the package into memory and initialize (later will be described in detail).

Declarations of 0 or more classes

0 or more interface declarations

Remember that each Java source file can contain definitions of multiple classes or interfaces, but at most one class or interface is public and the Java source file must be named with the name of the class of the public type. If this is the case, the source file name is not named after the class name, then the error occurs at compile time.

The order of the package and import statements is fixed, and in the Java source file, it must be in the first row (the comment line is not counted), followed by the import statement, followed by the declaration of the class or interface.

Ha ha!!! HelloWorld

 Public class helloworld{       publicstaticvoid  main (string[] args) {            System.out.println ("Hello,welcome to Java!!! ");}       }   
View Code

  Although the code above is very simple, it is a portal to the Java application, and each Java application runs from the main () method. Public---access rights; static method---, Main (string[] args)---parameter limit, void---return type, args is just the parameter of the main () method, is a String array, It is also possible to name the parameter names other than other names, and because the static modifier method defaults to the final type (which cannot be overridden by the quilt Class), it is also possible to precede main () with final.

To pass parameters to the main side, when running a Java application using Java commands, the command line can pass the parameter format to the Main method: Java classname [args ...], such as the Java Apple parameter1 parameter2, Parameters are Parameter1 and parameter2, the length of the args array is 2, and if no arguments are passed then the length of args is 0, you can write code test, here I do not show.

Comment Statements---> comments can be added anywhere in the Java source file and the Java compiler ignores comments in the program. Available in three different forms:

...---from//to the end of the bank are all annotated and the compiler ignores.

The compiler ignores the/*...*/---as comments from the beginning of "/*" to the end of "*/".

/**...*/between the---is a comment and the compiler ignores it. If such annotations appear before any declarations (such as classes, member variables of a class, or declarations of member methods), they are used as Javadoc document content.

The Java language keyword, I will not typing out, cut a small map.  

Create an Object--new

Quote--this, Super

Method return type--void

Each keyword has a special meaning and has its own mission. For example, the class keyword is used for the declaration of classes, the Void keyword is a method that does not return a value, and the following commonly used keywords are introduced slowly.

The Java language contains keywords that have reserved keywords that are reserved, although they are not currently used as keywords but may later be used, are const and Goto.

Use Java keywords to note: All the keywords are lowercase; friendly, sizeof is not a Java keyword; the identifiers in the program cannot be named by keywords.

The identifier is mentioned above, so what is an identifier? It refers to the name of a package, class, interface, variable, or method in a program. The Java identifiers must conform to the following naming conventions:

1. The first letter of the identifier must be the letter, the line "_", the dollar Sign "$".

2. The identifier consists of a number (0-9), an uppercase letter (A-Z), a lowercase letter (A-Z), a draw line "_", and a dollar sign.

3. Identifiers cannot be keywords and reserved words.

4. There is no length limit for identifiers.

5, the identifier is case sensitive.

The above mentioned the rules of identifiers, Java programming also has a certain specification, the main points are:

First, the class name and interface name---capitalized, and if the class name or interface name consists of several words, the first letter of each word is capitalized and the remaining letters are lowercase. such as Redapple.

Second, the method name and variable name---the first letter lowercase, also if it consists of a few words, in addition to the first word lowercase, the rest of the first letter of each word capitalized, the remaining words lowercase, such as getinstance, and if the variable name refers to the number of entities is a plural form, then the variable name is plural.

Again, the package name---all lowercase, such as Com.jang.

Finally, the constant name---all uppercase, if it is made up of multiple words, then the words are separated by a line between them, so that each word can be easily identified.

  Note: The naming rules of identifiers must be adhered to, otherwise it will lead to compilation errors, and for the programming specification is only recommended to abide by the habit, even if non-compliance will not lead to compilation errors, but the recommendation is to follow the good, after all, after a long time accumulation.

Slowly...

Create a Java source file and explain the knowledge involved

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.