Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JLS myths ~ if-then-else statement ~

JLS myths ~ if-then-else statement ~

HASUNUMA Kenji

October 01, 2016
Tweet

More Decks by HASUNUMA Kenji

Other Decks in Programming

Transcript

  1. if-else statement if ( someObject.eval() ) if ( otherObject.eval() )

    func1(); else func2(); assume that someObject.eval() is false. which is run, func1 or func2?
  2. if-else statement if ( someObject.eval() ) if ( otherObject.eval() )

    func1(); else func2(); assume that someObject.eval() is false. Neither func1 nor func2 is run.
  3. 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!
  4. From JLS (Java SE 8) IfThenStatement: if ( Expression )

    Statement IfThenElseStatement: if ( Expression ) StatementNoShortIf else Statement IfThenElseStatementNoShortIf: if ( Expression ) StatementNoShortIf else StatementNoShortIf
  5. 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?
  6. 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