Format convention
1. [Mandatory] Convention on the use of braces. If the braces are empty, simply write {} without line breaks; if is a non-empty code block:
1) There is no line break before the left brace.
2) Newline after the opening brace.
3) Line break before the right brace.
4) If there is else code after the right brace, there will be no line break; it means that the line must be broken after terminating the right brace.
2. [Mandatory] There should be no space between the left bracket and the following character; similarly, there should be no space between the right bracket and the previous character
. For details, see the correct example tips below Article 5.
3. [Mandatory] Spaces must be added between reserved words such as if / for / while / switch / do and the left and right brackets.
4. [Mandatory] A space must be added around any operator.
Explanation: Operators include assignment operator =, logical operator &&, addition, subtraction, multiplication and division symbols, ternary operators, etc.
5. [Mandatory] Use 4 spaces for indentation and tab characters are prohibited.
Note: If you use tab indentation, you must set 1 tab to 4 spaces. When IDEA sets tabs to 4 spaces, do not check Use tab character; in eclipse, insert spaces for tabs must be checked .
Positive example: (involving 1-5 points)
public static void main(String args[]) { // 缩进 4 个空格 String say = "hello"; // 运算符的左右必须有一个空格 int flag = 0; // 关键词 if 与括号之间必须有一个空格,括号内的 f 与左括号,0 与右括号不需要空格 if (flag == 0) { System.out.println(say); } // 左大括号前加空格且不换行;左大括号后换行 if (flag == 1) { System.out.println("world"); // 右大括号前换行,右大括号后有 else,不用换行 } else { System.out.println("ok"); // 在右大括号后直接结束,则必须换行 } }
6. [Mandatory] The number of characters in a single line is limited to no more than 120. If it exceeds, a new line is required. Follow the following principles when wrapping lines:
1) The second line is indented 4 spaces relative to the first line. Starting from the third line, there will be no further indentation. Refer to the example.
2) The operator is wrapped together with the following.
3) The dot symbol of the method call is wrapped together with the following.
4) If multiple parameters are too long, wrap after the comma.
5) Do not break lines before parentheses, see counterexample.
Positive example:
StringBuffer sb = new StringBuffer(); //超过 120 个字符的情况下,换行缩进 4 个空格,并且方法前的点符号一起换行 sb.append("zi").append("xin")... .append("huang")... .append("huang")... .append("huang");
Counter example:
StringBuffer sb = new StringBuffer(); //超过 120 个字符的情况下,不要在括号前换行 sb.append("zi").append("xin")...append ("huang"); //参数很多的方法调用可能超过 120 个字符,不要在逗号前换行 method(args1, args2, args3, ... , argsX);
7. [Mandatory] When defining and passing in method parameters, multiple parameters must be followed by commas Spaces are required.
Positive example: In the following example, the actual parameter "a" must be followed by a space.
method("a", "b", "c");
8. [Mandatory] The text file encoding of the IDE is set to UTF -8; the newline character of the file in the IDE is used Unix format, Do not use windows format.
9. [Recommendation] There is no need to add a number of spaces to align the characters of a certain line with the corresponding characters of the previous line.
Positive example:
int a = 3; long b = 4L; float c = 5F; StringBuffer sb = new StringBuffer();
Explanation: Increase the variable sb. If alignment is required, add a, b, and c. A space is a cumbersome thing in situations where there are many variables.
10. [Recommended] Insert a blank line between the execution statement group, variable definition statement group, different business logic or different semantics in the method body. There is no need to insert blank lines between the same business logic and semantics.
Note: There is no need to insert multiple lines of spaces to separate them.