I compiled both examples, and the byte codes received are identical:
public boolean test(boolean var); 0 iload_1 [var] 1 ifeq 6 4 iconst_1 5 ireturn 6 iconst_0 7 ireturn Line numbers: [pc: 0, line: 5] [pc: 4, line: 6] [pc: 6, line: 8] Local variable table: [pc: 0, pc: 8] local: this index: 0 type: scratch.Else1 [pc: 0, pc: 8] local: var index: 1 type: boolean public boolean test(boolean var); 0 iload_1 [var] 1 ifeq 6 4 iconst_1 5 ireturn 6 iconst_0 7 ireturn Line numbers: [pc: 0, line: 5] [pc: 4, line: 6] [pc: 6, line: 8] Local variable table: [pc: 0, pc: 8] local: this index: 0 type: scratch.Else2 [pc: 0, pc: 8] local: var index: 1 type: boolean
How to use the one you should use? In this contrived example, the answer sounds NEVER. The best code that matches the behavior is as follows:
public boolean test(boolean var){ return var; }
Given the choice of "if {return} else {return}" vs. "if {return} return", my answer is usually the last, because I prefer less indentation. But I think it really depends on how much code is in each branch. More than another code points to the last, more if the code points to the first.
source share