-2

I'm trying to understand ternary operators and don't see an example with return statements.

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

How would you write that without the ternary operator? Is it just:

if (next == null) { 

} else { 
  return (current,next);
Tim
  • 41,901
  • 18
  • 127
  • 145
chatslayer
  • 47
  • 3
  • *don't see an example with return statements* why would that affect the workings? – Tim Mar 02 '17 at 15:20
  • see documentation http://stackoverflow.com/documentation/java/118/basic-control-structures/2806/ternary-operator#t=201703021604546865206 – Venom Mar 02 '17 at 16:06

3 Answers3

5

Your version:

  • Completely removes one of the return values
  • Completely ignores the function call in the other
if (next == null) {
    return current;
} else {
    return reversing(current,next);
}

That said, the else isn't necessary. I'd put the early return on null on its own:

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

return reversing(current, next);
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
3
return (next == null) ? current : reversing(current, next);

is equivalent to

if (next == null) {
    return current;
} else {
    return reversing(current, next);
}
Culp
  • 41
  • 4
2

No. You would write as follows

if (next == null) {
    return current;
} else {
    return reversing(current, next);
}
nbro
  • 15,395
  • 32
  • 113
  • 196