29

In pages like http://en.wikipedia.org/wiki/?: the ternary/conditional operator ?: seems to be used for conditional assignments. I tried to use it for method calling, like this:

(condition) ? doThis() : doThat();

Both methods return void. Java tells me it is not a statement.

So, I'm guessing I can't do conditional method calling... or can I?

neXus
  • 2,005
  • 3
  • 29
  • 53
Saturn
  • 17,888
  • 49
  • 145
  • 271

3 Answers3

24

Think of ternary operators like a method in this case. Saying a ? b : c is (for the intents and purposes you're considering, see lasseespeholt's comment) equivalent to calling the pseudocode method:

ternary(a, b, c)
    if a
        return b
    else
        return c

which is why people can say things like x = a ? b : c; it's basically like saying x = ternary(a, b, c). When you say (condition) ? doThis() : doThat(), you're in effect saying:

if condition
    return doThis()
else
    return doThat()

Look what happens if we try to substitute the methods for what they return

 if condition
    return ???
 else
    return ???

It doesn't even make sense to consider it. doThis() and doThat() don't return anything, because void isn't an instantiable type, so the ternary method can't return anything either, so Java doesn't know what to do with your statement and complains.

There are ways around this, but they're all bad practice (you could modify your methods to have a return value but don't do anything with what they return, you could create new methods that call your methods and then return null, etc.). You're much better off just using an if statement in this case.

EDIT Furthermore, there's an even bigger issue. Even if you were returning values, Java does not consider a ? b : c a statement in any sense.

Jodaka
  • 1,237
  • 8
  • 16
  • 1
    "Saying a ? b : c is equivalent to calling the pseudocode method" <- Nope. Consider the situation where `b` or `c` has side-effects. So you need to consider those as lambdas as you do in your later examples. – Lasse Espeholt Sep 23 '12 at 17:48
  • True. I'll clarify that I'm simplifying. – Jodaka Sep 23 '12 at 17:50
  • 4
    @Jodaka, I consider your edit as the correct answer because it's not really about returning a value, it's about Java doesn't consider a ? b : c as a statement. – kzidane Dec 24 '13 at 20:33
2

The ternary operator is simply syntactic sugar.
It makes code easier to read/write, but it does not add real functionality.
Its primary use was to compress several lines of code into a single line, and was very useful when building Strings that differ only slightly, based on some conditions.

eg.

Collection<?> col = ...
System.out.println("There " + (col.size()==1 ? "is" : "are") + " "
     + col.size() + " " + (col.size()==1 ? "element" : "elements")
     + " in the collection");

instead of

Collection<?> col = ...
String message = "There ";
if(col.size()==1)
    message += "is";
else
    message += "are";
message += " "+col.size()
if(col.size()==1)
    message += " element";
else
    message += " elements";
message += " in the collection";
System.out.println(message);

As you can see, it simplifies the code.
(note: In the second example it is better to use StringBuilder instead of String concatenation)

But since (condition) ? doThis() : doThat(); (without return values) has the same meaning as if(condition) doThis(); else doThat(); there would be two ways of writing the same thing, without adding functionality. It would only complicate things:

  • for programmers: code is not uniform
  • for the implementation of the ternary operator: it now has to also support void methods

So No, the ternary operation can not be used for conditional method calling. Use if-else instead:

if(condition)
    doThis();
else
    doThat(); 
neXus
  • 2,005
  • 3
  • 29
  • 53
1

The ternary (conditional) operator returns a value. If your methods don't, they can't be used as parts of the operator (where it takes the value).

In order to understand it better, let's think of one simple binary operator: +. It works this way:

<eval1> + <eval2>  -->  <value>

It needs of 2 evaluable parts, and returns another. If you typed

doThis() + doThat();

or even

gimmeAValue = doThis() + doThat();

it would fail, as neither doThis() nor doThat() evaluate to anything (they "return" void). Of course, both <eval1> and <eval2> must be of some "compatible" type in order the + operator can handle them and return a value of some type.

Now let's see the ternary operator:

<evalBoolean> ? <eval1> : <eval2>  -->  <value>

It takes 3 evaluable parts, and returns a value.

The first evaluable part must be understandable (castable) by the compiler as a boolean. It will be used to decide which of the other 2 evaluable parts has to be returned.

The other two evaluable parts must be, well... evaluable. To something. Of some type.

In other words: the ternary conditional operator is intended to return something, not as code branching. Used this way:

gimmeAValue = testMe() ? returnThis() : returnThat();
J.A.I.L.
  • 10,644
  • 4
  • 37
  • 50