Lecture 2 Java Basics

1.3 The first Java program

1.3.1 Architecture of java program

[ Example 1-1 ]  A program that finds the sum of two numbers

public class Hello //class description

{

        public static void main(String args[])//Main method

        {    

            int x,y,sum; 

            x=123;

            y=456;                                 

            sum=x+y;                     

            System.out.println

            (" sum = "+ sum);

        }

}

Description: The general structure of a Java program is:

1. Java programs are mainly divided into two parts: class description and class body.

2. One of the main components of a Java program class body is a method (function).

3. oneA Java program has at least one class, in which there can be at most one (or none) main() method called the main method. Its format is fixed:

       public static void main(String args[])

4. The main method is the entry point of the Java program. That is, the program starts executing in main() and ends in main().

5. The "{ }" in main() indicates the scope of the method body. The body of the method is mainly composed of statements. Generally speaking, the statement ends with ";".

6. When the Java source program is stored, its file name must be the class name, and its extension must be ".java", such as the above program, we will save it as the Hello.java file (ie: the file name must be the same as the (main) class) The name should be the same, including uppercase and lowercase letters).

7. Convention: When defining a class, the first letter of the class name should be capitalized .

8. When writing Java , except pure Chinese characters, other characters (symbols) are in English, including punctuation marks.

1.4 Vocabulary and symbols of the Java language

1. Keywords 

 A keyword is a special vocabulary with a specific meaning specified by the Java language , which is unique and unchangeable, and is also commonly referred to as a reserved word. Usually lowercase letters. Keywords are often used in type definitions , statement definitions , etc. Its form is not exactly similar to C\C++.

2. Identifier

Java stipulates that identifiers can only consist of letters, numbers, underscores , dollar signs and Chinese characters , but cannot start with numbers . Identifiers can be used in programs as variable names, method names, and other naming symbols . 

The following identifiers are legal:

a,x$, $x,BOOK 1,sum5w_e3   

The following identifiers are illegal: 3s , s*T , -3x , bowy-1

It is also important to note:

①Keywords  cannot be used as identifiers , but can be used as part of identifiers .

Java is strictly case-sensitive for identifiers . For example b OOK and book are two different identifiers. Java agrees that variable names and method names generally start with a lowercase letter, and class names generally start with an uppercase letter. The length of the Java identifier is generally six digits . Must be a character in the Unicode character set.   

3. Annotation

Comments are descriptions of programs, and their purpose is to improve the understandability and readability of the program, thereby improving the maintainability of the program. Comments will not be executed when the program is running and do not occupy compilation space.

①Single  line comment

The symbol "//" means a single-line comment, starting from the "//" symbol until the end of the line or until the newline mark will be regarded as the content of the comment (the newline part is invalid).

②Multi  -line comments

The symbol "/* */" represents a multi-line comment, and no matter how many lines there are between the symbols "/*" and "*/" , it is regarded as the comment content.

③  Documentation comments

Documentation comments are represented by the symbol "/** */" . Like multi-line comments, the content between the symbols "/**" and "*/" is regarded as comment content no matter how many lines there are.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326038797&siteId=291194637