Unusual Java Conditional Syntax

We just discovered, a colleague and I, a weird compilation syntax with conditional If syntax :

if (true); {
  foo();
}

Does anyone here explain this weird syntax to us? thank.

+5
source share
7 answers

The first part if (true);is just a do-nothing condition that ends with ;. The rest is a call foo()inside a new visibility block. You must find what is foo()always called.

+6
source

if , , ( if). , foo :

if (false); {
    foo();
}
+8

, .

if (true) 
{    
    // do nothing 
}
{   
    foo(); 
}

{} foo(); . , {}, .

{
   int i=0;
   System.out.println(i);
}
{
   String i="hello";
   System.out.println(i);
}

.

+4

,

if (true) {};
foo();

stat if , foo ({}) ...

+1

, , - , ...

, if, .

+1

, (.. ), foo(), . foo() . , , , :

  • , ​​( , )
  • , , , ( "foo();" ),
  • () , , ,
  • It remains after the debugging session, and in this case it must be restored to what it was before the change was made.
+1
source

This dead code. The if block is useless.

Equivalent

if(doesn't matter whats here) {}
foo();
0
source

All Articles