Java code specifications-typographical layout, naming-and an example

Source: Internet
Author: User

I. Typographical specifications
1: when assigning values to variables during initialization, = needs to be aligned, so it looks neat and comfortable.
[Java]
// Before
Private Char joe = 'job ';
<Pre name = "code" class = "java"> private Char johnny = 'Johnny ';
<Pre name = "code" class = "java"> private Char quaid = 'quaid ';

// After
<Pre name = "code" class = "java"> private Char joe = 'job ';
<Pre name = "code" class = "java"> private Char johnny = 'Johnny ';
<Pre name = "code" class = "java"> private Char quaid = 'quaid ';
</Pre>
<Pre> </pre>
<Pre> </pre>
<Pre> </pre>
<Pre> </pre>
<Pre> </pre>
</Pre>

2: Add a space between keywords or variables and operators:
 
[Java] view plaincopy
For (int I = 0; I <10; I ++ ){
Int iCont = 1;
}

3: Empty lines are added between relatively independent code blocks and blocks. For example, empty lines are added between methods.

4: long statements and expressions should be written into multiple books

5: The main method is written at the bottom of the entire class.
[Java]

Ii. Naming rules
1: package name, all lowercase letters
2: Class Name and member variable name. The method name follows the hump rule.
3: The name of the Static Final variable in the Class. The names of the static Final variable should all be capitalized, and the complete meaning should be indicated.
 


3. annotation Specification
1: class. The method annotation is automatically generated by eclipse. /**
After a complete declaration of a class or method is written, the class and method are entered in English (spaces are not allowed ).
After entering:/**, the system will automatically generate their annotation template.
Note: The function description must be written for the next line.


Example 4

[Java]
/**
* For Java coding format recommendations, see JDK source code and Hyperic HQ source code (formerly known as spring open-source software, now vmware ).
* @ Author lihzh (coder)
* Address: http://mushiqianmeng.blog.51cto.com/3970029/737120
*/
Public class CodeRule {

// Declare the variable with spaces on both sides of the equal sign.
Private static int I = 1;

// Method declaration. There are spaces between the right and left braces.
Public static void main (String [] args ){
// If statement, the comparison connector (>) has spaces, and the parentheses and braces have spaces in the middle.
// If and left parentheses have spaces in the middle
If (I> 0 ){
System. out. println (I );
}
// Connect the two conditions (&) with spaces on the left and right.
If (I> 0 & I <2 ){
System. out. println (I );
}

// If. else statement format
// 1. Refer to JDK for personal usage. else and braces have spaces before and after.
If (I> 0 & I <2 ){
System. out. println (I );
} Else if (I> 2 ){
System. out. println (I + 1 );
} Else {
System. out. println (I );
}
// 2. Refer to the Hyperic HQ source code. else starts a new line and still has Spaces
If (I = 1 ){
System. out. println (I );
}
Else {
System. out. println (I );
}

// While Statement, which is of the same type as if statement. while and parentheses have spaces. The format inside the brackets is the same as if.
While (I> 0 & I <2 ){
System. out. println (I );
I ++;
}

// For statement, two formats
// 1. For more information, see Hyperic HQ. The semicolon is followed by a space. In each substatement, the connector is left or right with a space.
// For and parentheses have spaces in the middle and the size Brackets have spaces in the middle.
For (int j = 0; j <10; j ++ ){
System. out. println (I );
}
// 2. See JDK. The difference is that there is no space between the connector and left in the substatement.
For (int j = 0; j <10; j ++ ){
System. out. println (I );
}

// +-*/, Format. There are spaces before and after the four Arithmetic Operators.
// In some JDK Code, there is no space before or after the four arithmetic operators in the parameter passing of the method call or the existence of the four arithmetic operations in the judgment statement.
// In order not to cause confusions or confusions, spaces are reserved for all individuals.
Int a = 1 + 2;
Int B = 1-2;
Int c = 1*2;
Int d = 1/2;

// Ternary expression format, with spaces in the middle of each symbol
Int j = I> 2? 1:-1;

// Method description and call. The parameters are separated by commas (,) and there are spaces after commas.
Sum (a, B );
Sum (c + d, j );
} Www.2cto.com

// Method declaration, multiple parameters, with spaces after comma
Private static int sum (int I, int j ){
Return I + j;
}


}


Author: Allen_Zhao_2012

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.