-4

Java has ternary operator, that works on variables:

(a > 0) ? b++ : c--;

What is a, b, c were functions? I tested something like (a returns boolean, b & c are void):

a() ? b() : c(); 

but even my Eclipse does not allow that.

How close could I get to this programmatically (to generate functionality to make this with minimum amount of lines)?

I know in other languages like Javascript I could just pass function as parameter, but I have not met similar in Java.

Edit

We have gone in discussion to sidewals because of bad constellation of question, but here is what I want, and it is a real world problem:

What I want to achieve is single-liner to call second or third function based on return value of the first.

With minimum amount of code of course.

What I had tried was that ternary that is made to other things, that I know now.

12
  • 5
    You can only use methods that return a value as operands of the ternary conditional operator, since that operator must produce a value. Commented Jul 31, 2018 at 10:08
  • 3
    If a() returns a boolean and b() and c() return the type that shall be assigned respectively, it should work. Commented Jul 31, 2018 at 10:09
  • 2
    The ternary operator is not a replacement for the if statement. It is supposed to evaluate expressions, not execute arbitrary statements. If you used methods with return types, a() ? b() : c() would be legal, because it could be evaluated, not just executed. Otherwise, what you want is an if statement. Commented Jul 31, 2018 at 10:09
  • 2
    what about if (a()) b(); else c();? Commented Jul 31, 2018 at 10:09
  • 1
    "my Eclipse does not allow that" - Why not? Is there an error? What is that error? How have you confirmed that error is because of the conditional operator? What testing and experimentation have you done to validate this? "I know in other languages like Javascript I could just pass function as parameter" - That's true, but what does that have to do with the conditional operator? I think you're making some incorrect assumptions or have some fundamental misunderstandings about what you're trying to do. Commented Jul 31, 2018 at 10:11

3 Answers 3

1

As others have already explained, the conditional operator is an expression that evaluates to a value, and can thus not be applied to void methods.

If you need a one-liner discarding any return values, use a simple if-else:

if (a()) b(); else c();
Sign up to request clarification or add additional context in comments.

Comments

1

If you go through Java Ternary Operator let's you assign a value to a variable based on a boolean expression — either a boolean field, or a statement that evaluates to a boolean result (Its used for assignment). In above you are trying to call functions with return value void that is not allowed in Ternary Operator in java.

The ternary operator, also known as the conditional operator, can be used as an alternative to the Java if/then/else syntax

Comments

0

You can only use the ternary operator as an expression, not a statement. It's not simply alternative syntax for an if-else - the point is that if a() returns true, the entire expression resolves to the result of b(), else the entire expression resolves to the result of c().

As such, it doesn't make sense if b and c return void. They must return something, and the most specific type that they share will be the resulting type of the whole expression.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.