Blank lines can improve readability by grouping sections of the code that are logically related . A blank line should also be used in the following places:
after the copyright block comment, package declaration, and import section;
between class declarations;
between method declarations;
between the last field declaration and the first method declaration in a class; and
before a block or single-line comment, unless it is the first line in a block.
A single blank space (not tab) should be used:
between a keyword and its opening parenthesis. This applies to the following keywords: catch, for, if, switch, synchronized while. It does not apply to the keywords super and this;
after any keyword that takes an argument. Example: return true;
between two adjacent keywords;
between a keyword or closing parenthesis, and an opening brace "{";
before and after binary operators except .(dot);
after a comma in a list; and
after the semicolons in a for statement, e.g.:
for (expr1; expr2; expr3) {.
Blanks should not be used:
between a method name and its opening parenthesis;
before or after a .(dot) operator;
between a unary operator and its operand;
Between a cast and the expression being casted;
After an opening parenthesis or before a closing parenthesis; and
After an opening square bracket [ or before a closing square bracket ].
Examples:
a += c[i + j] + (int)d + foo(bar(i + j), e); a = (a + b) / (c * d); if (((x + y) > (z + w)) (a != (b + 3))) { return foo.distance(x, y); }
Do not use special characters like form-feeds or backspaces.
Line indentation is always 4 spaces, for all indentation levels.
The construction of the indentation may include tabs as well as spaces in order to reduce the file size ; however, you may not change the hard tab settings to accomplish this. Hard tabs must be set every 8 spaces.
Lines should be limited to 80 columns (but not necessarily 80 bytes, for non-English languages). Lines longer than 80 columns should be broken into one or more continuation lines, as needed. All the continuation lines should be aligned, and indented from the first line of the statement. The amount of the indentation depends on the type of statement.
If the statement must be broken in the middle of a parenthesized expression, such as for compound statements, or for the parameter list in a method invocation or declaration, the next line should be aligned with the first character to the right of the first unmatched left parenthesis in the previous line. In all other cases, the continuation lines should be indented by a full standard indentation (4 spaces). If a class or method declaration has one or more continuation lines, then a single blank line should immediately follow the opening brace.
Examples:
if (long_logical_test_1 long_logical_test_2 long_logical_test_3) { statements; } function(long_expression1, long_expression2, long_expression3, long_expression4, long_expression5, long_expression6);
A continuation line should never start with a binary operator. Never break a line where normally no white space appears, such as between a method name and its opening parenthesis, or between an array name and its opening square bracket. Never break a line just before an opening brace {. Examples:
// WRONG while (long_expression1 long_expression2 long_expression3) { } // RIGHT while (long_expression1 long_expression2 long_expression3) { }