2017-03-02 94 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); 
}