Can Java's ternary/conditional operator (?:) be used to call methods instead of assigning values?

て烟熏妆下的殇ゞ 提交于 2019-12-17 07:30:06

问题


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?


回答1:


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.




回答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(); 



回答3:


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();


来源:https://stackoverflow.com/questions/12554547/can-javas-ternary-conditional-operator-be-used-to-call-methods-instead-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!