Java Learning to organize notes (ii) Java BASIC syntax structure

Source: Internet
Author: User
Tags uppercase letter

First, the preparation of the source files

Java is a fully object-oriented language, so all Java operations are done based on class. The code for all the programs in Java needs to be placed in a class, the class is declared with the keyword class, some modifiers can be added before class, and the source file of the Java application consists of several classes that are independent of the writing form.

1. Example:

 //  Hellodate.java  import  java.util.Date; //  public  class       hellodate{ /*   * The purpose of this program is to output the current system time  */ public< /span> static   main (string[] args) {System.out.print ( "Hell,it ' s" );      System.out.println ( new   Date ()); }}    

(1) The import statement is a function of introducing an additional class or a class package. Note that it is "extra" because there is a class package that is automatically introduced into every Java program file, which is the Java.lang package.

Import java. util. The role of date is to introduce the date class in the Java.util package.

If you do not want to introduce multiple classes in the same package one by one, you can use "*" to represent all classes in the package, such as import java.util.*.

(2) class is declared with the keyword class name, which must be the same as the file name. The preceding keyword public indicates that this is a common class that can be accessed by outsiders.

(3) class hellodate is called a type declaration, followed by the first curly brace and the last curly brace and the content between them called the class body.

(4) There is at least one class with the same name as the primary file, and the class must contain a main () function, which must be in the form of the following:

 Public Static void Main (string[] args) {    ...   // Program Code }

Where the keyword public indicates that this is an externally exposed function, passing in the parameter of Main () is an array of string objects.

(5) This program does not use args, but the Java compiler requires that the main method must be declared, because args is used to store "command-line arguments."

(6) Java allows multiple main methods in several classes of a program. However, if a program has more than one class, the main () of the class that is called by the command line is awakened-the main method must be public, and the class it belongs to does not matter whether it is public. (As you can understand: public static void Main (string[] args) This code is the entry, start) of the program

System.out.println (new Date ());

(7) The function of the System.out.println is to output a statement to the screen and wrap the line. The parameter passed in here is an object of the date class, and the object is created with the new action method.

(8) "Why does the output not look like this ' [email protected] ' is an object's address (reference), but a string? "Because every non-primitive type object in the Java class Library has a toString () method, this function is called when the compiler wants to get a string and only those objects are on hand." If you want to write a class that also has this behavior, just write a tostring method for it.

(9) Once this statement is executed, the object is no longer used and becomes garbage (garbage).

In 2.Java there is a so-called "garbage collection mechanism (garbage Collection)", which monitors all objects created through new and frees the memory space occupied by objects when they are no longer referenced, so there is no need to worry about excessive garbage. Memory Leak (Leak) ".

3. Precautions:

(1) Java is strictly case-sensitive.

(2) A sequential string in a Java program cannot be written separately in two lines.

(3) Each line of statements is separated by a semicolon ";" Represents the end.

Ii. naming rules for source files

1.Java naming rules for source files:

(1) If there are multiple classes in the source file, there can be at most one class that is the public class.

(2) If a class is declared as public, the source file must have exactly the same name as the class, and the extension is. java.

(3) If the source file does not have a public class, the source file name is the same as the name of a class, and the extension is. java.

2.unwritten rules in Java:

(1) The name of the Java class usually begins with an uppercase letter. If the class name consists of multiple words, the first letter of each word should be capitalized, such as Datetest. If the class name contains a word abbreviation, each letter of the abbreviation should be capitalized, such as Xmlexample.

(2) The first word of the name of the method should start with a lowercase letter, and the following word begins with an uppercase letter, such as GetName. Except for the constructor function.

(3) The name of the constant should be capitalized and can represent the full meaning of the constant. If a constant name consists of multiple words, it is delimited by an underscore, such as max_size.

3. Good programming Habits:

(1) notice the indentation . The program block is written in indentation style, and it is not allowed to write multiple phrases in one line, that is, a single statement is written on a line.

(2) Focus on annotations. In general, the source program should have a valid code amount between 10% and 20%, and the comment should be similar to the code it describes. Comments on the code should be placed above or to the right (comments on a single statement) adjacent to the position, not placed below. In addition, in the actual project development, after modifying the code, it is necessary to modify the contents of the comments, keep the code and comments synchronized.

(3) name of the identifier. the name of the identifier should be sought to be known . Avoid misunderstandings by using complete words or abbreviations that you can understand. To name a variable, avoid using a single character.

4. Keywords and identifiers:

(1) Keywords are also called reserved words. In a program that has a specific meaning, Java is a predefined identifier, and a user-defined identifier cannot be the same as a keyword .

(2) An identifier is a name that represents a thing (or object) in a program. The names of packages, classes, methods, parameters, and variables in Java are identifiers.

The Java language specifies that identifiers can be made up of 4 characters, such as letter (case), number (0~9), underscore (_), and dollar sign ($), and the 1th character cannot be a number. Of course, a user-defined identifier cannot be the same as a keyword.

It is important to note thatJava is a case-sensitive language , with class and class, system, and system representing completely different symbols.

Third, comments and inline documents

1. Note (Comment) is a description of the function and purpose of the code. Writing the appropriate annotations in the program will make the program code easier to read and enhance the maintainability of the code.

Java offers the following three styles of annotation:

(1) Single-line comment. This style of annotation is derived from C + + for single-line comments, starting with//until the end of the line.

// This is a single-line comment

(2) Multi-line comment. This style of annotation was originally used by C, and C + + also follows this style. Start with/*, followed by the comment content can span multiple lines, and finally end with */.

/* Multi-line comment, which can span multiple lines */

(3) Documentation comments. Document comments begin with/**, and subsequent comments can span multiple lines and end with */. Some programmers prefer to start with * as each line in multiple lines, so you can usually see this:

/** * This is a document comment * You can generate an HTML description document by calling the Javadoc command */

2. Inline documents are documents and code in the same file. The document is marked with a special annotation syntax, which is then extracted from the annotated document by a tool.

Javadoc is the tool used to extract the embedded documents, which can be found in the bin directory of the JDK installation directory. This tool can not only extract the information identified by the special tag, but also extract the class name and function name that the information belongs to. That is, a standard description document.

The output of the Javadoc is an HTML document that can be read by a Web browser. Javadoc can also add user-defined HTML commands to the resulting document so that the user can beautify the document that is about to be generated according to their own ideas.

Java Learning to organize notes (ii) Java BASIC syntax structure

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.