Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolon at end of 'if' statement

Today, after half an hour of searching for a bug, I discovered that it is possible to put a semicolon after an if statement instead of code, like this:

if(a == b); // Do stuff 

Which basically means that the stuff will be done whether a equals b or not, and the if statement has no point whatsoever. Why doesn't Java give me an error? Is there any situation in which this would be useful?

like image 663
Erik Bergsten Avatar asked Jan 01 '13 17:01

Erik Bergsten


People also ask

What happens if you put a semicolon at the end of an if statement?

Do not place a semicolon on the same line as an if, for, or while statement. Do not use a semicolon on the same line as an if , for , or while statement because it typically indicates programmer error and can result in unexpected behavior.

Which statement must not end with semicolon?

Control statements ( if , do , while , switch , etc.) do not need a semicolon after them, except for do ...

How do you end an if statement?

An IF statement is executed based on the occurrence of a certain condition. IF statements must begin with the keyword IF and terminate with the keyword END. Components within IF statements can be connected with the keywords AND or OR.

What happens if we put semicolon after if statement in Java?

It's the semicolon! if you put a semicolon directly after the condition in an if statement, Java thinks it's finished with the body of the statement. The indentation of the next line, which is so important to human readers, is ignored by Java.


2 Answers

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:

    • Findbugs
    • Checkstyle
    • Pmd

    They can instantly highlight problems such as this one.

I would recommend to combine both solutions.

like image 65
Marcin Szymczak Avatar answered Sep 21 '22 02:09

Marcin Szymczak


Is there any situation in which this would be useful?

Useful? As in "makes your code cleaner, clearer, faster, more maintainable"? Not at all. This is most likely poor, confusing code.

But it's not necessarily benign. Such a statement can perform actions and/or alter state due to methods which cause side effects, and optionally evaluate those methods due to short-circuiting of operators.

if( a() && b() ); 

Here, a() or b() may do something, and b() will only execute if a() is true.

As to why, I think the answer is simply that it would be worse to deviate from defined, expected behavior (e.g. statements like while(reader.read());) than the alternative of developers writing bad code.

Writing bad code is always possible. And just to reiterate, this would be bad code in almost any case.

like image 31
Tim M. Avatar answered Sep 24 '22 02:09

Tim M.