An unexplained empty block or statement makes the code less readable. It might also indicate missing code, a misplaced semicolon, or a misplaced brace. For these reasons, it should be avoided.

If a block is empty because some code is missing, add the code.

If an if statement has an empty then branch and a non-empty else branch, it may be possible to negate the condition and move the statements of the else branch into the then branch.

If a block is deliberately empty, add a comment to explain why.

In the following example, the while loop has intentionally been left empty. The purpose of the loop is to scan a String for the first occurrence of the character '='. A programmer reading the code might not understand the reason for the empty loop body, and think that something is missing, or perhaps even that the loop is useless. Therefore it is a good practice to add a comment to an empty block explaining why it is empty.

  • Help - Eclipse Platform: Java Compiler Errors/Warnings Preferences.
  • Java Language Specification: 14.2 Blocks, 14.6 The Empty Statement, 14.9 The if Statement, 14.12 The while Statement, 14.13 The do Statement, 14.14 The for Statement.