-2

Is it possible to use a shorthand for ternary assignment expressions of the type

boolean x = false;
// ... code ...
a = x ? b : a;   // Assigning a to a is pointless
a += x ? 1 : 0;  // Adding 0 to a is pointless

I'm thinking something along the lines of

a = x ? b;   // Assign b to a if x is true
a += x ? 1;  // Add 1 to a if x is true

Not that it saves a lot of typing, I'm just curious if something like this exists. I just recently discovered the null coalesce operator in PHP 7, which does something kind of similar. To me it looks much cleaner than

if( x ) a = b;
if( x ) a += 1;

since these are assignments, and using a ternary expression looks more natural when reading from left to right. The supposed duplicate question isn't about assignments specifically, which in my opinion are a special use case of the ternary operator.

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • 3
    Use the `if` statement. That's why Java provides it. – nbro Feb 20 '17 at 14:24
  • By the way, Java is a pragmatic language which tries to avoid alias and multiple choices for the user; instead, it focuses on simplicity (even though it may seem strange to someone reading this comment, but this is the feeling that I've regarding Java after having used it for a few years). – nbro Feb 20 '17 at 14:32
  • What stops you from including that very same line inside one of your Java classes and checking by yourself that it doesn't compile? – walen Feb 20 '17 at 14:33
  • @walen Nothing stops me from doing that, and I actually have - it gives me a syntax error. I'm not asking if the provided _hypothetical_ examples compile or not, I'm asking if there is any such language construct in Java. What's stopping you from actually thinking for a second about what the question is before posting useless comments? – Magnus Feb 20 '17 at 15:34
  • The answer to your question is "No, Java has no conditional assignment operator." You seem to be confusing conditional assignments with conditional values. `a = x ? b : a` is an unconditional assignment, `a` is always assigned the value of the ternary operator `x ? b : a`. – Old Pro Feb 21 '17 at 08:24

1 Answers1

1

Unfortunately this is not supported by Java.

From the official oracle doc:

Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson).
This operator is also known as the ternary operator because it uses three operands.
In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

The following program, ConditionalDemo2, tests the ?: operator:

class ConditionalDemo2 {

    public static void main(String[] args){
        int value1 = 1;
        int value2 = 2;
        int result;
        boolean someCondition = true;
        result = someCondition ? value1 : value2;

        System.out.println(result);
    }
}

Because someCondition is true, this program prints "1" to the screen.
Use the ?: operator instead of an if-then-else statement if it makes your code more readable;
for example, when the expressions are compact and without side-effects (such as assignments).

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

JDurstberger
  • 4,127
  • 8
  • 31
  • 68