JLS myths

~ if-then-else statement ~
HASUNUMA Kenji

k.hasunuma@coppermine.jp

GlassFish Users Group Japan
if-else statement
if ( someObject.eval() )

if ( otherObject.eval() )

func1();

else

func2();
assume that someObject.eval() is false.

which is run, func1 or func2?
if-else statement
if ( someObject.eval() )

if ( otherObject.eval() )

func1();

else

func2();
assume that someObject.eval() is false.

Neither func1 nor func2 is run.
if-else statement
if ( someObject.eval() )

if ( otherObject.eval() )

func1();

else

func2();
It's if-else's short-circuit.

Don't be misled by source code format!
From JLS (Java SE 8)
IfThenStatement:
if ( Expression ) Statement

IfThenElseStatement:
if ( Expression ) StatementNoShortIf 

else Statement

IfThenElseStatementNoShortIf:
if ( Expression ) StatementNoShortIf 

else StatementNoShortIf
Statement
Statement:
StatementWithoutTrailingSubstatement 

LabeledStatement 

IfThenStatement 

IfThenElseStatement 

WhileStatement 

ForStatement
StatementNoShortIf
StatementNoShortIf:
StatementWithoutTrailingSubstatement 

LabeledStatementNoShortIf



IfThenElseStatementNoShortIf 

WhileStatementNoShortIf 

ForStatementNoShortIf
*** 'IfThenStatementNoShortIf' don't exist ***
if-else statement (fixed)
if ( someObject.eval() ) {

if ( otherObject.eval() )

func1();

} else

func2();
assume that someObject.eval() is false.

which is run, func1 or func2?
if-else statement (fixed)
if ( someObject.eval() ) {

if ( otherObject.eval() )

func1();

} else

func2();
func2 is run!

because it use block as statement in outer
if statement
JLS myths

~ If-then-else statement ~
HASUNUMA Kenji

k.hasunuma@coppermine.jp

GlassFish Users Group Japan

JLS myths