Source: http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html,
Http://morningspace.51.net/,moyingzz@etang.com.
1 Introduction (Introduction)
1.1 Why coding standards (Why Have Code Conventions)
Coding standards are especially important for programmers for the following reasons:
-80% of the cost for a software life cycle is maintenance.
-Almost no software is maintained by the original developer throughout its lifecycle.
-Coding specifications can improve the readability of software and allow programmers to understand new code as soon as possible.
-If you release the source code as a product, you need to ensure that it is well packaged and clear, just like any other product you have built.
In order to implement the specification, each software developer must comply with the encoding specification. Everyone.
1.2 copyright statement (Acknowledgments)
This document describes the coding standards in the Java language specification of Sun MicroSystems. Major contributors include Peter King, Patrick Naughton, Mike DeMoney, Jonni Kanerva, Kathy Walrath, and Scott Hommel.
This document is now maintained by Scott Hommel. Please send comments to shommel@eng.sun.com
2. File Names)
This section lists common file names and their suffixes.
2.1 File suffix (File Suffixes)
Java programs use the following file suffixes:
File category file suffix
Java source file. java
Java bytecode file. class
2.2 Common File Names)
Common file names include:
Object Name Purpose
The preferred file name for GNUmakefile makefiles. We use gnumake to create (build) software.
README overview preferred file names for files contained in a specific directory
3. File Organization)
A file consists of paragraphs separated by blank lines and Optional comments that identify each paragraph. Programs with more than 2000 rows are hard to read and should be avoided as much as possible. The "Java source file example" provides a Java program example with a reasonable layout.
3.1 Java Source Files)
Each Java source file contains a single public class or interface. If private classes and interfaces are associated with a public class, you can put them and public classes in the same source file. The public class must be the first class or interface in this file.
Java source files also follow the following rules:
-Start annotation (see "Start annotation)
-Package and Import Statement (see "package and Import Statement)
-Class and interface declaration (see "class and interface declaration )"""
3.1.1 start with Comments (Beginning Comments)
All source files should start with a C-language annotation, listing the class name, version information, date, and copyright statement:
/** Classname ** Version information ** Date ** Copyright notice */3.1.2 Package and Import Statement (Package and Import Statements)
In most Java source files, the first non-Comments line is a package statement. After that, you can introduce the statement. For example:
Package java. awt; import java. awt. peer. CanvasPeer; 3.1.3 Class and Interface declaration (Class and Interface Declarations)
The following table describes the parts of the class and interface declaration and their order of appearance. For more information, see "example of Java source files with annotations. "
Annotation of each part of the class/interface declaration
Class 1/interface document annotation (/**...... */) The information to be included in the comment. For more information, see "document comment"
Declaration of two types or interfaces
Annotations for Class 3/interface implementation (/*...... */) If necessary, the comment should contain any information about the entire class or interface, which is not suitable for commenting on the class/interface document.
Class 4 (static) variables are first the public variables of the class, followed by the protection variables, then the variable at the package level (no access modifier, access modifier), and finally the private variables.
5. The instance variables are at the public level first, followed by the protection level, followed by the package level (no access modifier), and finally the private level.
6 Constructor
7. These methods should be grouped by functions, rather than scope or access permissions. For example, a private class method can be placed between two public instance methods. The purpose is to facilitate reading and understanding the code.
4. Indentation)
Four spaces are often used as a unit of indentation. The exact description of indentation is not specified in detail (space vs. Tab ). A tab is equal to eight spaces (not four ).
Line Length)
Avoid a line with more than 80 characters, because many terminals and tools cannot handle it well.
Note: The example used in the document should use a shorter length, which generally cannot exceed 70 characters.
4.2 wrap (Wrapping Lines)
When an expression cannot be contained in a row, it can be disconnected according to the following general rules:
-Disconnected after a comma
-Disconnected before an operator
-Select a higher-level disconnection, rather than a lower-level disconnection.
-The new row should be aligned with the beginning of the expression of the previous row at the same level.
-If the above rules cause your code to be chaotic or make your code pile up on the right side, place them with eight spaces.
The following are examples of method call disconnection:
Values (longExpression1, longExpression2, longExpression3, longExpression4, longExpression5); var = values (longExpression1, values (longExpression2, longExpression3); the following are two examples of disconnecting arithmetic expressions. The former is better, because the disconnection is located outside the bracket expression, which is a higher level of disconnection.
LongName1 = longName2 * (longName3 + longName4-longName5) + 4 * longname6; // PREFFER longName1 = longName2 * (longName3 + longName4-region) + 4 * longname6; // AVOID the following is an example of two indent method declarations. The former is a general situation. If the latter uses the regular indent method, it will move the second and third rows to the right, so it will be indented with 8 spaces.
// Conventional indentation someMethod (int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother ){...} // INDENT 8 spaces to avoid very deep indents private static synchronized horkingLongMethodName (int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother ){...} the line feed of if Statements usually uses eight spaces, because regular indentation (4 spaces) makes the statement body look hard. For example:
// DON't use this indentation if (condition1 & condition2) | (condition3 & condition4) |! (Condition5 & condition6) {// bad wraps doSomethingAboutIt (); // make this line easy to miss} // use this indentation instead if (condition1 & condition2) | (condition3 & condition4) |! (Condition5 & condition6) {doSomethingAboutIt () ;}// or use this if (condition1 & condition2) | (condition3 & condition4) |! (Condition5 & condition6) {doSomethingAboutIt ();} There are three feasible methods for processing the ternary expression:
Alpha = (aLongBooleanExpression )? Beta: gamma; alpha = (aLongBooleanExpression )? Beta: gamma; alpha = (aLongBooleanExpression )? Beta &