Learn Java from the beginning, some essays and information to help you remember
A naming convention
1) Package: Names should all be nouns or noun phrases, all lowercase, between words with "." The general use of the company/organization website domain name reverse followed by the specific software internal module name
Package Naming example: Packages Com.sun.java; Com.baidu.java;
2) class: noun or noun phrase, the first letter of each word capitalized; generally do not use abbreviations, unless their abbreviations are more general and easy to understand, the big hump nomenclature, each word's initials are capitalized, such as HTML, when you want to distinguish between the interface and implementation of the class, you can add "Impl" after the class, For example: Interface container and class Containerimpl;exception classes use "Exception" as the end of the class name, for example: datanotfoundexception; abstract class with "abstract "As the beginning of the class naming, for example: The Abstractbeandefinition,abstractbeanfactory;test class is best able to use" Test "as the end of the class naming. Example: Containertest
Class Naming example: Class person; class Springrain
3) Interface: Naming rules with "class"
Example of interface naming: interface Runner;
4) Methods: verb or moving parts of speech, first letter lowercase, the second and later the first letter of the word capital, the small hump name method, the first word starts with a lowercase letter, the first letter of the second word capitalized;
Method Naming example: Run (); Displayinformation ();
5) Variable: noun or noun phrase, first letter lowercase, second and subsequent words capitalized;
Do not advocate the use of the following line "_" and the dollar symbol "$" as a variable at the beginning of the word can be separated by the line, the variable name should not be too long but also meaningful, unless it is temporarily used (for example, only a few lines of code) is discarded after the case, do not recommend using a single letter variable name Commonly used temporary variable names include i,j,k,m,n that represent integers and C that represent letters, and E, which represents the exception object.
Example of variable naming: int age; int student_age; String studentname; Exception e;
6) Constant: Each word should be capitalized, the words are separated by "_".
Example of constant naming: final static min_width = 4;final static default_container_size = 20;
7) Name of the file: the Java source program file ends in. java, and the compiled file ends with a. Class. For example: Container.java,container.class.
Summary: The first letter of the class name should be capitalized. The first letter of the field, method, and object (handle) should be lowercase. For all identifiers, all the words contained in it should be close together, and uppercase the first letter of the middle word. If a constant initialization character appears in the definition, all letters in the static final base type identifier are capitalized. This can be used to mark the constants that they belong to the compile period. The Java package is a special case: they are all lowercase letters, even if the middle word is the same. For domain name extension names, such as Com,org,net or EDU, all should be lowercase.
Ii. Organization of documents
1. A file consists of a paragraph separated by a blank line and an optional comment identifying each paragraph. A program with more than 2000 lines is difficult to read, so the number of lines of code in a Java program file cannot exceed 2000 lines, unless there are special reasons. Each Java source file contains a single public class or interface. If private classes and interfaces are associated with a common class, they can be placed in the same source file as the public class. The public class must be the first class or interface in this file.
The Java source file also follows these rules, which specify the order of the Java program paragraphs:
-Opening Notes
-Package and Introduction statements
-Class and interface declarations
2. In a Java class, the order of the parts:
1) Attribute declaration
2) Construction Method declaration
3) Static statement
4) General method declaration
5) Main method declaration
6) Declaration of the Inner class
Summary: Generally a complete Java file content is as follows:
1) Definition of package
2) Impot Class (Enter the order of the package, avoid using *) input package should follow the Java.*.*,javax.*.*,org.*.*, com.*.* Order import, in the import should not use * (for example: java.util.*)
3) Definition of class or interface
4) static variable definition, according to Public,protected,private order
5) instance variable definition, by public,protected,private order
6) Construction method
7) method definition order according to public method (class own method), implement interface method, overloaded public method, protected method, package scope method and private method.
Note: The number of lines of code for each method in a generic class does not exceed 100 lines.
Three separators and indents
1. Use blank lines
Use a single line of blank lines to separate them in the following cases:
1) between the methods
2) between the logical paragraph subsections of the method's internal code
3) After a local variable is declared in the method, the specific Java statement precedes
4) before the comment line
5) separate multiple classes/interfaces defined in the same source file using two rows of blank lines
2. Using whitespace
In addition to the normal components separated by space characters (such as data types and variable names), a space character should be used to delimit the following cases:
1) between operators and operators, such as: C = a + B;
2) after the comma in the argument list, such as: void M1 (int year, int month) {}
3) After if,for,while for statements such as: if (a > B) {}
3. Use line break
It is not recommended to write multiple statements on a line, the length of a statement is generally more than 80 characters, should be wrapped;
When a row of expressions cannot be displayed on a single line, the following order is required to split the lines:
1) after the "(" or "=" symbol, split the line
2) in "," to split the line
3) split the line after an operator
4) indent concurrent splits at the same level
5) If you encounter "(" when splitting again in the split line, the newly split line should be placed at a further indent level
4. Use parentheses
You can use parentheses to dictate the order of operations
5. Use curly Braces
The start position can be at the end of the current content, or another row
6. Using indentation
Apply indentation in the following cases
1) composition in the class
2) Composition in the method body or statement block
3) Non-starting line when swapping lines
Reduction is generally based on the first level of the composition of the indentation of four spaces, and Eclipse default format is 1 tab, you can change the following way:window--> preferences......--> java--> Code styl E--> Formatter Point to the right of the "Edit ..." button, switch to the first tab "indentation", in the general Settings group to change the tab policy from Table only to Space only, while the I Ndentation size and Tab size to 4, and finally the top profile name to take a name, any name, then click OK button on it. Pressing the TAB key later or Ctrl + Shift + F automatically takes 4 spaces as a level indent. In the Eclipse development tool, after writing the code, you can use the shortcut key: CTRL + SHIFT + F to automatically format the code.
Four Declaration statements
1. Variable declaration: Each row declares a variable and adds a comment. For example:
int count; Number of containers
int count, size; AVOID this!
Array declarations should be prefixed. For example:
Int[] table;
String[] args;
2. class or interface declaration
1) The "{" and declaration statements are on the same line.
2) If it cannot be displayed on the same line, then "extends" or "implements" are split and placed after two indent levels.
3) The "}" symbol should be on its own line.
For example:
public class Chiefexecutiveofficer
Extends Manager
Implements person {
...
}
3. Method declaration
The "{" and the declaration statements are on the same line.
The "}" symbol should be on its own line.
Summary: Each row declares a variable and, if possible, initializes it while declaring the variable, unless its initial value is not yet determined. Local variables should be declared at the beginning of the method or block of statements in which they reside, and should not be accompanied by declarations.
Five notes
Java has two types of annotation methods. "/* This is a comment */" or "//This is a comment"
The first should be used to write Javadoc, and all begin with "/**".
The second one is suitable for commenting on part of the code, but only for the very short content.
Java Naming conventions