2017-03-02 98 views
-2

我想了解三元運算符,並沒有看到與返回語句的例子。閱讀和理解三元操作符?

return (next == null) ? current : reversing(current,next); 

如果沒有三元運算符,你會怎麼寫?難道僅僅是:

if (next == null) { 

} else { 
    return (current,next); 
+0

*沒有看到收益報表爲例*爲什麼會影響運作? –

+0

請參閱文檔http://stackoverflow.com/documentation/java/118/basic-control-structures/2806/ternary-operator#t=201703021604546865206 – basslo

回答

2

號你可以寫成如下

if (next == null) { 
    return current; 
} else { 
    return reversing(current, next); 
} 
5

您的版本:

  • 完全消除返回一個值
  • 完全忽略在函數調用其他
if (next == null) { 
    return current; 
} else { 
    return reversing(current,next); 
} 

也就是說,else是沒有必要的。我把上null早日迴歸自身:

if (next == null) { 
    return current; 
} 

return reversing(current, next); 
3
return (next == null) ? current : reversing(current, next); 

相當於

if (next == null) { 
    return current; 
} else { 
    return reversing(current, next); 
}