阅读和理解三元运算符?

时间:2017-03-02 15:18:15

标签: java ternary

我试图了解三元运算符,并且没有看到返回语句的示例。

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

如果没有三元运算符,你会怎么写?它只是:

if (next == null) { 

} else { 
  return (current,next);

3 个答案:

答案 0 :(得分:5)

您的版本:

  • 完全删除其中一个返回值
  • 完全忽略其他
  • 中的函数调用
if (next == null) {
    return current;
} else {
    return reversing(current,next);
}

那就是说else没有必要。我将null的早期回报单独归结为:

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

return reversing(current, next);

答案 1 :(得分:3)

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

相当于

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

答案 2 :(得分:2)

没有。你会写如下

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