What is better? if..else or multiple simple if

前端 未结 5 1699
旧巷少年郎
旧巷少年郎 2020-12-19 08:44

talking about java performance .. what is better? if..else or multiple simple if

if( condition ) {
  some_code;
  return value;
}
else if( condition ) {
  so         


        
5条回答
  •  伪装坚强ぢ
    2020-12-19 09:13

    The if...else example you give doesn't need the returns if this is in a function with no return value. In that case, the if...else will be easier to read.

    Furthermore, the if...else should be preferred because it makes explicit that these cases are mutually exclusive.

    If there's a performance difference here, then your compiler/interpreter sucks.

提交回复
热议问题