Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Ternary without Assignment

Is there a way to do a java ternary operation without doing an assignment or way to fake the assingment?

I like how succinct ternary code looks when doing a bunch of if/then/elses.

I'm hoping to be able to call one of two void functions based on a boolean algebra statement.

Something like:

(bool1 && bool2) ? voidFunc1() : voidFunc2();

My functions are of return type void, so if there is a way to fake this in an assignment to make it work, then I"m okay with that... I would like to see how to do it though :)

like image 780
James Oravec Avatar asked Apr 12 '13 17:04

James Oravec


People also ask

Can you use a ternary without assignment?

Nope you cannot do that.

How do you use ternary instead of if else?

The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way. First, you need to write a conditional expression that evaluates into either true or false .

Can we use assignment operator in ternary operator?

both are fine. invalid lvalue in assignment. which gives error since in C(not in C++) ternary operator cannot return lvalue.

Does ternary need else?

A ternary operation is called ternary because it takes 3 arguments, if it takes 2 it is a binary operation. It's an expression returning a value. If you omit the else you would have an undefined situation where the expression would not return a value. You can use an if statement.


1 Answers

Nope you cannot do that. The spec says so.

The conditional operator has three operand expressions. ? appears between the first and second expressions, and : appears between the second and third expressions.

The first expression must be of type boolean or Boolean, or a compile-time error occurs.

It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.

[EDIT]

Since you asked about reflection, here's a solution. I'm not recommending this. I'm posting it only because you asked.

public class MyCall {      public void a(){System.out.println("a");}     public void b(){System.out.println("b");}      public static void main(String... args)     {         new MyCall().go();     }      public void go()     {         Class<? extends MyCall> class1 = this.getClass();         Method aMethod = class1.getMethod("b", null);         Method bMethod = class1.getMethod("a", null);         Object fake = false ? aMethod.invoke(this, null) : bMethod.invoke(this, null);         Object fake2 = true ? aMethod.invoke(this, null) : bMethod.invoke(this, null);     } } 

At the end of the day you've got to ask yourself if being succint improves your code's readability (think for-each loop). None of these solutions improve the code's readability IMHO. If I were you I'd rather go with this.

if(condition)     a(); else     b(); 

I'm actually for including braces even when loops only contain a single line, but since you're going after crisp code, the snippet above should do.

like image 157
Deepak Bala Avatar answered Oct 07 '22 14:10

Deepak Bala