Java Programming specification
Standardized programming is the beginning of good code writing style, code legibility and coding style has a great relationship, so the coding is the basis of the specification, but also a large project development must be a capability.
This article is a total of four sections:
1. Naming conventions for Java
2. Module order in Java files
3. Separating and indenting
4. Comments
One, the Java naming convention 1, the package
General lowercase, with "." Separate, use the Company/organization network domain name/specific software internal module
Package Naming example: Packages Com.sun.java; com.mycompany.db;
2. Class
The first letter of each word is capitalized, generally not abbreviated.
Interface: Generally add "Impl" after the class name, for example: interface Containerimpl
Abstract class: Use "abstract" as the beginning of the class naming, for example: Classabstractbeanfactory
Test class: Typically add "Test" after the class name, for example: Class Containertest
Exception class: Generally add "Exception" after the class name, for example: datanotfoundexception
3. Methods
First letter lowercase, followed by the first letter of each word capitalized, for example: Run (); Displayinformation ();
4. Variables
The first letter is lowercase, followed by each first letter capitalized, the general habitual in front with a "M", for example: Minflater
5. Constants
Each word is capitalized, and the word is concatenated with "_", for example: final static min_width = 4;final staticdefault_container_size = 20;
Second, the module order within the Java file
It is best not to have more than 2000 lines of code in a Java file, or the program will be difficult to read.
1. The order of the paragraphs in the Java file: opening comments, imported packages, class and interface declarations
Opening comment
Imported Packages: It is best not to use "*" when importing, do not use import java.javax.*
class and interface declarations: by public,protected,private Order
2. The order of the parts in the Java class:
A) attribute declaration
b) Construction Method declaration
c) Static statement
d) General method declaration
e) Main method declaration
f) Inner class
In general, the code of a method in a class should not be more than 100 lines.
Three, separating and indenting 1, using blank lines
A) between the methods
b) before the comment
2. Use space
A) between the operator and the operator, such as: C = a + B;
b) After the comma in the argument list, such as: void M1 (int year, int month) {}
c) After the IF, For,while statement such as: if (a > B) {}
Four, note 1, the Java language two kinds of annotation methods:
/* This is a comment */
This is a comment
The first one will be used on the Javadoc, starting with "/**"
Second short comment for part of the code
Java Programming specification