Java learning notes (2) Basic Java syntax structure and basic java syntax
I. Compiling source files
Java is a fully object-oriented language, so all Java operations are completed based on classes. The Code of all programs in Java needs to be placed in a class. The class is declared with the keyword class. Some modifiers can be added before the class, the source file of a Java application is composed of several classes with independent writing forms.
1. Example:
// HelloDate. javaimport java. util. date; // introduce java. in the util package, the Date class public class HelloDate {/** is used to output the current System time */public static void main (String [] args) {System. out. print ("Hell, it's"); System. out. println (new Date ());}}
(1) The purpose of the import Statement is to introduce 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, that is, the java. lang package.
Import java. util. Date is used 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) the class must be declared with the keyword class. The class name must be the same as the file name. The previous keyword "public" indicates that this is a public class and can be accessed by the outside world.
(3) class HelloDate is called a class declaration, and the first and last braces and the content between them is called a class body.
(4) There must be at least one class with the same name as the main file, and this class must contain a main () function. The form must be as follows:
Public static void main (String [] args) {... // program code}
The public keyword indicates that this is an open function. The input parameter of main () is a String object array.
(5) This program does not use args, but the Java compiler requires that the main method be declared in this way because args is used to store "command line parameters ".
(6) Java allows multiple main methods in multiple classes of a program. However, if a program has multiple classes, only the main () of the class called by the command line will be awakened-the main method must be public, its class does not matter whether it is public. (This can be understood as follows: the public static void main (String [] args) code is the entry and start of the program)
System.out.println(new Date());
(7) The function of System. out. println is to output a statement to the screen and wrap it. The input parameter is an object of the Date class, which is created using the new operation method.
(8) "Why isn't the output result 'java. util. Date @ de6ced 'like this the address of an object (reference), but a string ?" This is because every non-basic type object in the Java class library has a toString () method. When the compiler wants to get a String, but only those objects are available, this function will be called. If you want your own classes to have such behavior, you just need to write a toString Method for it.
(9) after the statement is executed, the object will no longer be used and become garbage ).
2. there is a so-called "Garbage Collection mechanism" in Java, which will monitor all the objects generated through new one by one, and release the Memory space occupied by this object when these objects are no longer referenced, so there is no need to worry about the "Memory Leak" caused by excessive garbage )".
3. Notes:
(1) Java is case sensitive.
(2) A continuous string in a Java program cannot be written in two separate lines.
(3) Each line of statements ends with a semicolon.
Ii. Source File naming rules
1. Naming rules for Java source files:
(1) If the source file contains multiple classes, only one class can be public.
(2) If a class is declared as a public class, the source file name must be exactly the same as the class name and the extension is. java.
(3) If the source file does not have a public class, the source file name must be the same as the name of a class and the extension is. java.
2. Unwritten Rules in Java:
(1) The name of a Java class usually starts with an uppercase letter. If the class name is composed of multiple words, the first letter of each word should be uppercase, such as DateTest. If the class name contains the abbreviation, each letter of the abbreviation should be in uppercase, such as XMLExample.
(2) the first word of the method name should start with a lowercase letter, followed by a letter starting with an uppercase letter, such as getName. Except constructors.
(3) The constant name should all use uppercase letters, and can indicate the complete meaning of the constant. If a constant name is composed of multiple words, it is separated by underscores, such as MAX_SIZE.
3. Good Programming habits:
(1) Pay attention to indentation. The program block is written in an indent style. You cannot write multiple short sentences in one line, that is, write only one statement in one line.
(2) Pay attention to comments. Under normal circumstances, the valid code of the source program should be between 10%-20%, and the comments should be similar to the code described. Comments to the code should be placed above it or the right side (comments to a single statement) adjacent location, not below. In addition, in actual project development, after modifying the code, you must modify the content of the comment accordingly to keep the code and comments synchronized.
(3) Name the identifier. The identifiers should be named with good name and knowledge. Use complete words or abbreviations that everyone can understand to avoid misunderstanding. Do not use a single character for variable naming.
4. keywords and identifiers:
(1) A keyword is also called a reserved word. It has a specific meaning in the program. Java predefined identifiers, and user-defined identifiers cannot be the same as keywords.
(2) An identifier is the name of a transaction (or object) in a program. The names of packages, classes, methods, parameters, and variables in Java are all identifiers.
The Java language specifies that the identifier can only contain letters (uppercase and lowercase), numbers (0 ~ 9), underscore (_), and dollar sign ($), and the 1st characters cannot be numbers. Of course, user-defined identifiers cannot be the same as keywords.
Note that Java is a case-sensitive language. class, Class, System, and system represent completely different symbols.
Iii. Annotations and embedded documents
1. Comment is a description of the functions and usage of the Code. Compiling appropriate comments in the program will make the program code easier to read and enhance the maintainability of the Code.
Java provides the following three annotation styles:
(1) single line comment. This annotation style originates from C ++ and is used for single-line comments starting with // until the end of the row.
// This is a single line comment
(2) multi-line comments. This annotation style was initially used by C, and C ++ also follows this style. It starts with/*, followed by comments that can span multiple rows and end.
/* Multi-line comment, which can span multiple rows */
(3) document comments. A document annotation starts with/**. The subsequent annotation content can also span multiple lines and end. Some programmers like to start with * in multiple rows, so we can usually see the following code:
/*** This is a document annotation * You can call the javadoc command to generate an HTML Description document */
2. embedded documents are the documents and code in the same file. The document is marked by special annotation syntax and extracted from it using a tool.
Javadoc is a tool used to extract embedded documents. You can find this tool in the bin directory of the JDK installation directory. This tool not only extracts information identified by the special mark, but also extracts information such as the class name and function name of the information. A standard instruction document.
The output of javadoc is an HTML document, which can be read through a Web browser. Javadoc can also add user-defined HTML commands to the generated documents, so that users can beautify the documents to be generated based on their own ideas.