Semicolon at end of ‘if’ statement

Why does it happen?

Java Language Specification says that:

The Empty Statement

An empty statement does nothing.

EmptyStatement:
    ;

Execution of an empty statement always completes normally

It essentially means that you want to execute empty statement if a==b

if(a == b);

What should you do:

There are two main solutions to this problem:

  1. You can avoid problems with empty statement by using code formatter
    and surrounding stuff inside if with { and }. By doing this
    Your empty statement will be much more readable.

    if(a == b){
      ;
    }
    
  2. You can also check tools used for static code analysis such as:

    They can instantly highlight problems such as this one.

I would recommend to combine both solutions.

Leave a Comment