阅读和理解三元运算符?

cha*_*yer -2 java ternary

我试图理解三元运算符,但没有看到返回语句的示例.

 return (next == null) ? current : reversing(current,next);
Run Code Online (Sandbox Code Playgroud)

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

if (next == null) { 

} else { 
  return (current,next);
Run Code Online (Sandbox Code Playgroud)

Dav*_*ton 5

你的版本:

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

那就是说,else没有必要.我会尽早回归null:

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

return reversing(current, next);
Run Code Online (Sandbox Code Playgroud)