12

I made a coding mistake while working on an application, it was a test for null reference. It took me hours to discover what the issue was, but what i don't understand is why the code behaved this way.

String name = null;
String value = null;

if(name != null && value != null);
{
    System.out.println("Values not null");
}

The if Statement ended with ;, that was my mistake and the Values not null was printed even when it is obvious that both values are null. Can anybody explain why?

0

9 Answers 9

14

This semicolon ends a statement (an empty one), so your code is translated by the compiler to something like this:

if(name != null && value != null)
{
  //nothing here
}
{
  System.out.println("Values not null");
}

In other words, if if expression is true, it executes empty block of code. Then no matter whether if was true or not, the runtime proceeds and runs the block containing System.out. Empty statement is still a statement, so the compiler accepts your code.

Another place where such a mistake can happen:

for(int i = 0; i < 10; ++i);
{
  System.out.println("Y U always run once?");
}

or even worse (infinite loop):

boolean stop = false;
while(!stop);
{
  //...
  stop = true;
}

It took me hours to discover what the issue was

Good IDE should immediately warn you about such statement as it's probably never correct (like if(x = 7) in some languages).

6
  • i used netbeans but it didn't warn me. Oct 7, 2012 at 19:48
  • @TomaszNurkiewicz.. An IDE will not warn you about this.. It is not considered as incorrect use of if..
    – Rohit Jain
    Oct 7, 2012 at 19:54
  • 3
    @TomaszNurkiewicz Eclipse warns you. It calls it Dead Code. Oct 7, 2012 at 19:55
  • 1
    Well it seems that my Eclipse is cheating on me.. It doesn't warn me..
    – Rohit Jain
    Oct 7, 2012 at 19:58
  • @RohitJain: turns out both IntelliJ and Eclipse do warn. I am not saying it's incorrect (it compiles), but it's suspicious and obscure. Oct 7, 2012 at 19:58
5

; terminates the statement.

if(name != null && value != null);

is equivalent to:

if(name != null && value != null)
{
}

And the following code is simply a block of code.

2
if(name != null && value != null);
{
    System.out.println("Values not null");
}

is equivalent to:

if(name != null && value != null){}

{
    System.out.println("Values not null");
}
2
if(name != null && value != null);

Here your if statement got finished due to semi-colon...

So, it is just like an empty if block: -

if(name != null && value != null) {}

So your next code becomes an Initializer block: -

{
    System.out.println("Values not null");
}

Since it is an initializer block.. It will be executed always no matter what your if condition evaluated to..

1

It's the standard grammar rules. Semicolons terminate statements, but statements may be empty, so these lines:

if (...);
{
}

are equivalent to:

if (...) /* do nothing */ ;

{
    ...
}

The following { ... } statement just looks like any other code block, and will always be executed, because the if statement is done with by then.

1

Your code is equivalent to:

if(name != null && value != null) {
   // empty code block
}

System.out.println("Values not null");
1

The semi-colon ends the statement, and thus detaches the if from the following block.

Note that blocks can be arbitrary.

{
System.out.println("This is some code");
}

if(true); This is valid, and it does nothing. The language allowing this is pretty stupid, although IDEs should catch these issue with warnings and the like.

0

One bare ; is a whole empty statement. After the if there has to be exactly one statement that will be executed when if is true. The one statement that is executed in your case is the empty statement. You can combine more statements to one with {statement; statement;...}

0

Understand that conditional statements needn't always follow with opening and closing braces. Most languages will allow you to simply emit the braces if you only want to execute a single statement when the terms of the condition are met.

Meaning that the following is completely valid, for instance:

if (var1 == true && var2 == true) var3 = 'both true';
{
    //block here, this is always executed regardless of the statements outcome
}

Essentially, your code does exactly this. Except your single statement is determined to be ; (yes, this is considered a statement).

What you wrote is the same as:

if(name != null && value != null) {
    // do nothing
}

System.out.println("Values not null");

Not the answer you're looking for? Browse other questions tagged or ask your own question.