1. [Mandatory] Rules for braces. If there is no content, simply use {} in the same line. Otherwise:
  1) No line break before the opening brace.
  2) Line break after the opening brace.
  3) Line break before the closing brace.
  4) Line break after the closing brace, only if the brace terminates a statement or terminates a method body, constructor or named class. There is no line break after the closing brace if it is followed by else or a comma.

2. [Mandatory] No space is used between the ‘(‘ character and its following character. Same for the ‘)’ character and its preceding character. Refer to the Positive Example at the 5th rule.

3. [Mandatory] There must be one space between keywords, such as if/for/while/switch, and parentheses.

4. [Mandatory] There must be one space at both left and right side of operators, such as ‘=’, ‘&&’, ‘+’, ‘-‘, ternary operator, etc.

5. [Mandatory] Each time a new block or block-like construct is opened, the indent increases by four spaces. When the block ends, the indent returns to the previous indent level. Tab characters are not used for indentation.

Note: To prevent tab characters from being used for indentation, you must configure your IDE. For example, “Use tab character” should be unchecked in IDEA, “insert spaces for tabs” should be checked in Eclipse.

Positive example:

  1. public static void main(String[] args) {
  2. // four spaces indent
  3. String say = "hello";
  4. // one space before and after the operator
  5. int flag = 0;
  6. // one space between 'if' and '(';
  7. // no space between '(' and 'flag' or between '0' and ')'
  8. if (flag == 0) {
  9. System.out.println(say);
  10. }
  11. // one space before '{' and line break after '{'
  12. if (flag == 1) {
  13. System.out.println("world");
  14. // line break before '}' but not after '}' if it is followed by 'else'
  15. } else {
  16. System.out.println("ok");
  17. // line break after '}' if it is the end of the block
  18. }
  19. }

6. [Mandatory] Java code has a column limit of 120 characters. Except import statements, any line that would exceed this limit must be line-wrapped as follows:
  1) The second line should be intented at 4 spaces with respect to the first one. The third line and following ones should align with the second line.
  2) Operators should be moved to the next line together with following context.
  3) Character ‘.’ should be moved to the next line together with the method after it.
  4) If there are multiple parameters that extend over the maximum length, a line break should be inserted after a comma.
  5) No line breaks should appear before parentheses.

Positive example:

  1. StringBuffer sb = new StringBuffer();
  2. // line break if there are more than 120 characters, and 4 spaces indent at
  3. // the second line. Make sure character '.' moved to the next line
  4. // together. The third and fourth lines are aligned with the second one.
  5. sb.append("zi").append("xin").
  6. .append("huang")...
  7. .append("huang")...
  8. .append("huang");

Counter example:

  1. StringBuffer sb = new StringBuffer();
  2. // no line break before '('
  3. sb.append("zi").append("xin")...append
  4. ("huang");
  5. // no line break before ',' if there are multiple params
  6. invoke(args1, args2, args3, ...
  7. , argsX);

7. [Mandatory] There must be one space between a comma and the next parameter for methods with multiple parameters.

Positive example: One space is used after the , character in the following method definition.

  1. f("a", "b", "c");

8. [Mandatory] The charset encoding of text files should be UTF-8 and the characters of line breaks should be in Unix format, instead of Windows format.

9. [Recommended] It is unnecessary to align variables by several spaces.

Positive example:

  1. int a = 3;
  2. long b = 4L;
  3. float c = 5F;
  4. StringBuffer sb = new StringBuffer();

Note: It is cumbersome to insert several spaces to align the variables above.

10. [Recommended] Use a single blank line to separate sections with the same logic or semantics.

Note: It is unnecessary to use multiple blank lines to do that.