Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 884
Voldemort Avatar asked Sep 23 '12 17:09

Voldemort


People also ask

Can ternary operator be used without assignment?

Nope you cannot do that.

Can I call function in ternary operator?

Nope, you can only assign values when doing ternary operations, not execute functions.

Can we assign value in ternary operator?

The conditional ternary operator in JavaScript assigns a value to a variable based on some condition and is the only JavaScript operator that takes three operands. result = 'somethingelse'; The ternary operator shortens this if/else statement into a single statement: result = (condition) ?

What is ternary operator How can it be used instead of if-else statement?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.


2 Answers

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.

like image 140
Jodaka Avatar answered Oct 18 '22 17:10

Jodaka


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();  
like image 23
neXus Avatar answered Oct 18 '22 18:10

neXus