I personally organize the Java encoding format specifications, which is also the specification I have observed during development. The code format specification is only intended to reflect a standard and aesthetic. You can comply with your own specifications according to your own situation and company requirements.
- /**
- * For Java coding format recommendations, see JDK source code and Hyperic HQ source code. The famous open-source software under spring is 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 );
- }
- // Method declaration, multiple parameters, with spaces after comma
- Private static int sum (int I, int j ){
- Return I + j;
- }
-
- }
Note: Most of the format details can be set in the formater format of Eclipse. In the future, you only need to use ctrl + shift + F for formatting. For example, the if else format, the number of characters in each line is full, and so on.
This article from the "difficult coder" blog, please be sure to keep this source http://mushiqianmeng.blog.51cto.com/3970029/737120