简体   繁体   中英

Applying increment using ternary operator in Java

Is that possible to apply increment using a ternary operator in Java?

For example I want to make this without an "if" statement, not because it will more readable or shorter, just I want to know.

if(recordExists(recordId)){
   numberofRecords++;
}

Is that possible to apply increment using a ternary operator in Java?

You can use addition instead.

numberOfRecords += recordExists(recordId) ? 1 : 0;

IMHO This doesn't have side effects.

Is that possible to apply increment using a ternary operator in Java?

Well you could write:

// Ick, ick, ick.
int ignored = recordExists() ? numberOfRecords++ : 0;

Or make a no-op method call:

// Ick, ick, ick.
Math.abs(recordExists() ? numberOfRecords++ : 0);

I would strongly discourage you from doing so though. It's an abuse of the conditional operator. Just use an if statement.

The purpose of a conditional operator is to create an expression whose value depends on a condition.

The purpose of an if statement is to execute some statement(s) based on a condition.

To quote from Eric Lippert's tangentially-related C# blog post :

The purpose of an expression is to compute a value, not to cause a side effect. The purpose of a statement is to cause a side effect.

EDIT: Given that doubt has been cast over the validity of this answer:

public class Test {
    public static void main(String[] args) {
        boolean condition = true;
        int count = 0;
        int ignored = condition ? count++ : 0;
        System.out.println("After first check: " + count);
        Math.abs(condition ? count++ : 0);
        System.out.println("After second check: " + count);
    }
}

Output:

After first check: 1
After second check: 2

Keep in mind that ternary operators can do some strange things if you try to use increment in them. I believe the term for this is "short circuiting," and the results are very counter-intuitive.

Best to avoid!

For example note the following:

public class Strange {
    public static void main(String[] args) {
        int x = 1;
        x = x > 0 ? x++ : x--;
        System.out.println("x= " + x); // x is 1, both the increment and decrement never happen.

        int y = 5;
        y = y < 0 ? y++ : y--;
        System.out.println("y= " + y); // y is 5, both the increment and the decrement never happens.

        // Yet, in this case:

        int a = 1;
        int b = 2;

        a = a > 0 ? b++ : a--;
        System.out.println("a= " + a + " b= " +b); // a = 2, b = 3;
        // in this case a takes on the value of b, and then b IS incremented, but a is never decremented.

        int c = 5;
        int d = 1;
        int e = 0;

        c = c < 10 ? d++ : e--;
        System.out.println("c= " + c + " d= " + d + " e= " + e); // c = 1, but d = 2, and e = 0. 
        // c is assigned to d, then d in INCREMENTED, then the expression stops before evaluating the decrement of e!

        c = c > 10 ? d++ : e--;
        System.out.println("c= " + c + " d= " + d + " e= " + e);
        // c = 0, d = 1, e = -1

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM