Increment/Decrement and Conditional Operator


Operators

Operators in Java programming language are special symbols that perform specific operations on one, two, or three operands, and then return a result.


The Increment/Decrement Operators (++/--)

Variables can be incremented or decremented by 1 using the ++ and -- operators, respectively.

When the ++ and -- operators follow variables, they are called post-increment and post-decrement respectively.

int a = 10;
a++; // a now equals 11
a--; // a now equals 10 again

When the ++ and -- operators precede the variables the operations are called pre-increment and pre-decrement respectively.

int x = 10;
--x; // x now equals 9
++x; // x now equals 10

If the operator precedes the variable, the value of the expression is the value of the variable after being incremented or decremented. If the operator follows the variable, the value of the expression is the value of the variable prior to being incremented or decremented.

int x=10;
System.out.println("x=" + x + " x=" + x++ + " x=" + x); // outputs x=10 x=10 x=11
System.out.println("x=" + x + " x=" + ++x + " x=" + x); // outputs x=11 x=12 x=12
System.out.println("x=" + x + " x=" + x-- + " x=" + x); // outputs x=12 x=12 x=11
System.out.println("x=" + x + " x=" + --x + " x=" + x); // outputs x=11 x=10 x=10

Be careful not to overwrite post-increments or decrements. This happens if you use a post-in/decrement operator at the end of an expression which is reassigned to the in/decremented variable itself. The in/decrement will not have an effect. Even though the variable on the left hand side is incremented correctly, its value will be immediately overwritten with the previously evaluated result from the right hand side of the expression:

int x = 0;
x = x++ + 1 + x++; // x = 0 + 1 + 1
                   // do not do this - the last increment has no effect (bug!)
System.out.println(x); // prints 2 (not 3!)

Correct:

int x = 0;
x = x++ + 1 + x; // evaluates to x = 0 + 1 + 1
x++; // adds 1
System.out.println(x); // prints 3

The Conditional Operator (? :)

Syntax

{condition-to-evaluate} ? {statement-executed-on-true} : {statement-executed-on-false}

As shown in the syntax, the Conditional Operator (also known as the Ternary Operator1) uses the ? (question mark) and : (colon) characters to enable a conditional expression of two possible outcomes. It can be used to replace longer if-else blocks to return one of two values based on condition.

result = testCondition ? value1 : value2

Is equivalent to

if (testCondition) {
   result = value1;
} else {
   result = value2;
}

It can be read as "If testCondition is true, set result to value1; otherwise, set result to value2"

For example:

// get absolute value using conditional operator
a = -10;
int absValue = a < 0 ? -a : a;
System.out.println("abs = " + absValue); // prints "abs = 10"

Is equivalent to

// get absolute value using if/else loop
a = -10;
int absValue;
if (a < 0) {
   absValue = -a;
} else {
   absValue = a;
}
System.out.println("abs = " + absValue); // prints "abs = 10"

Common Usage

You can use the conditional operator for conditional assignments (like null checking).

String x = y != null ? y.toString() : ""; //where y is an object

This example is equivalent to:

String x = "";
if (y != null) {
   x = y.toString();
}

Since the Conditional Operator has the second-lowest precedence, above the Assignment Operators, there is rarely a need for use parenthesis around the condition, but parenthesis is required around the entire Conditional Operator construct when combined with other operators:

// no parenthesis needed for expressions in the 3 parts
10 <= a && a < 19 ? b * 5 : b * 7
 
// parenthesis required
7 * (a > 0 ? 2 : 5)

Conditional operators nesting can also be done in the third part, where it works more like chaining or like a switch statement.

a ? "a is true" :
b ? "a is false, b is true" :
c ? "a and b are false, c is true" :
    "a, b, and c are false"
 
//Operator precedence can be illustrated with parenthesis:
a ? x : (b ? y : (c ? z : w))

Basic Programs