Overview of This Lesson

Unary operators are operators that take only a single operand. Recall that binary operators will take 2 operands (usually one on the left and one on the right). In Java, there is a unary operator that allows you to increment a numeric variable, and one that allows you to decrement a numeric variable.

Pre-Requisites

Before starting this tutorial, make sure you've completed Expressions and Statements, and Arithmetic Expressions, and Augmented Assignment Operators.

Recall that the arithmetic operators you've learned so far, such as +, * and +=, are binary operators - they require two operands, one on each side. Unary operators require only 1 operand. Java has the following arithmetic unary operators:

Arithmetic Unary Operators
Operation Symbol Example
Increment ++ x++; // increments x by 1 (post-increment)
++x; // increments x by 1 (pre-increment)
Decrement -- x--; // decrements x by 1 (post-decrement)
--x; // decrements x by 1 (pre-decrement)

We'll discuss the difference between pre-increment/decrement and post-increment/decrement in a moment.

The code listing below demonstrates how the increment operator works:

public class UnaryOps {
    public static void main(String[] args) {
        int intNum1 = 2;

        System.out.print("intNum1: " + intNum1);
        System.out.println();

        intNum1++;
        System.out.println("increment intNum1: " + intNum2);
        intNum1++;
        System.out.println("increment intNum1: " + intNum2);
        intNum1++;
        System.out.println("increment intNum1: " + intNum2);
    }
}

The output for this program:

intNum1: 2  

increment intNum1: 3
increment intNum1: 4
increment intNum1: 5

The decrement operator works in much the same way, except that it subtracts 1 instead of adds 1:

public class UnaryOps {
  public static void main(String[] args) {
      int intNum1 = 2;

      System.out.print("intNum1: " + intNum1);
      System.out.println();

      intNum1--;
      System.out.println("increment intNum1: " + intNum2);
      intNum1--;
      System.out.println("increment intNum1: " + intNum2);
      intNum1--;
      System.out.println("increment intNum1: " + intNum2);
  }
}

The output for this program:

intNum1: 2  

increment intNum1: 1
increment intNum1: 0
increment intNum1: -1

Pre-Increment vs. Post-Increment

Unary operators in Java can go on the left side of the operator (pre-increment/decrement) or the right side of the operand (post-increment/decrement). The difference is in when the increment actually takes place. When the operator is on the left side of the operand, the variable is incremented/decremented before the variable's value is used. When the operator is on the right side of the operand, the variable's value is used before the variable is incremented/decremented. This might sound confusing until you witness an example.

Example

Try the following code in your editor:

int x = 1;
System.out.println(x++); // what does this output?
// reset x to 1
x = 1;
System.out.println(++x);  // what does this output?

If you run the code above, you'll notice that the first println() statement prints 1. Why doesn't it print 2, since x++ changes x to a 2? Because when the ++ operator is AFTER the operator, the original value of x (1) is sent to the println() method first, and then x's value is updated to 2 after.

In the last 2 statments, x is reset to a value 1. The last println() statement prints the expected value of 2 for x. That's because when the operator is BEFORE the variable, the incremented value of x is sent to println().

I always imagine it this way: when the operator comes first, the variable is incremented first. When the operator comes after, the variable is incremented after.

This behaviour also matters when you use the unary operators in a mathematical expression. For example: Given that x=5 and y=2 what do you think would be the value of result after the following assignment statement?

int result = 1;
result += ++x * y;

In this case, we would have to check the order of precedence of the augmented assignment operators: you would find that all assignment operators have the lowest precedence, so they will always evaluate last. This means that the order of operations in this statement would be:

  1. ++x - this adds 1 to the current value of x; x was 5, now it has a value of 6
  2. the new value of ++x (6) * y (2) results in a value of 12
  3. adding the result of the entire expression (12) to result (1), so 13 is stored into result

If you try these statements and print result, you'll see the ouput of 13. You can include the output of x if you'd like to confirm that it has incremented to a value of 6.

Notice the difference when you use the post-increment:

int result = 1;
result += x++ * y;

In this example, the order of operations changes:

  1. the original value of x (5) multiplied by y (2) resulting in 10
  2. x++ stores the new value of 6 into x
  3. the result of 10 is added to the result variable: result contained 1, add 10 to that and now result contains 11

If you try this example and print the value of result, you'll see it outputs 11. You can add the output of x to confirm that it did increment to a value of 6, even though the old value was used in the addition expression.

The decrement operator works in the same way. Feel free to try both examples using decrement instead of increment:

int result = 1;
result += --x * y;

This example outputs 1 for result (--x is 0, 0 * y is 0, adding 0 to result (1) yeilds 1)

int result = 1;
result += x-- * y;

This example outputs (x [1] is multiplied by y [2] first, resulting in 2, then x is decremented to 0; the result of 2 is added to result, which puts the value 3 in the result variable)

It's also important to note that like the augmented assignment operators, you can't use the increment or decrement operators unless the variable has been initialized to a value. For example, the following segment of code won't compile:

int x;
x++;

To fix it, the variable x must be initialized to a value:

int x = 1;
x++;