Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obscure conditional Java Syntax

We just found, a colleague and I, a strange compiling syntax with the If conditional syntax :

if (true); {
  foo();
}

Is there someone here for explaining to us this strange syntax? Thx.

like image 786
Patrick Ferreira Avatar asked Mar 02 '11 15:03

Patrick Ferreira


2 Answers

to me, it looks like an if statement with an empty body, followed by a code block that always executes (and is unrelated to the if statement). For example, does foo also execute here:

if (false); {
    foo();
}
like image 198
Kevin Avatar answered Sep 20 '22 09:09

Kevin


The first part, if (true); is just a do-nothing conditional that ends at the ;. The rest is a call to foo() inside a new scope block. You should find that foo() is always called.

like image 27
Bill the Lizard Avatar answered Sep 23 '22 09:09

Bill the Lizard