Sub topic 6
Unary Operators and Prefix versus Postfix
A unary operator is a symbol that works on one value. For example, in -4, the minus sign is a unary operator and 4 is its operand, or input value. The expression -4 produces the value negative four.
Java has unary operators that change a sign, reverse a true-or-false value, or update a variable by one. Some update operators have two forms: prefix and postfix. The difference matters when you use the value of the expression.
This chapter assumes you can write and run a simple Java program and know how to create variables.
Unary plus and minus
A variable is a named place that holds a value. An expression is a piece of code that produces a value. When Java evaluates an expression, it works out that value.
Unary plus, +, leaves a numeric value unchanged. Unary minus, -, reverses its sign:
- If
numberis4,+numberproduces4. - If
numberis4,-numberproduces-4. - If
numberis-4,-numberproduces4.
These expressions do not change the value stored in number. To change the variable, you must assign a value to it, as in number = -number.
The same symbols can also work on two values. In 4 - 2, the minus sign means subtraction. In -4, it is unary minus.
Parentheses can make the intended grouping clear. For example, -(2 + 3) produces -5, while (-2) + 3 produces 1.
Reversing a boolean value with `!`
A boolean is a value that is either true or false. The unary operator !, called logical NOT, produces the opposite boolean value.
| Expression | Result |
|---|---|
| `!true` | `false` |
| `!false` | `true` |
If a boolean variable named ready holds false, !ready produces true. It does not change ready. To store the opposite value, write ready = !ready.
Java requires a boolean value after !. For example, !5 is not valid Java.
Increment and decrement
The increment operator, ++, increases a numeric variable by one. The decrement operator, --, decreases it by one.
Both operators update the variable. They also produce a value that the rest of an expression can use. Their position decides which value they produce:
- Prefix puts the operator before the variable:
++countor--count. It produces the new value. - Postfix puts the operator after the variable:
count++orcount--. It produces the old value.
For example, suppose count starts at 3:
| Expression | Value produced | Value stored in `count` afterward |
|---|---|---|
| `++count` | `4` | `4` |
| `count++` | `3` | `4` |
| `--count` | `2` | `2` |
| `count--` | `3` | `2` |
Each row starts with a fresh count value of 3. In every row, the variable changes. The difference is whether the expression produces the value from before or after that change.
If the expression is a statement on its own, both positions have the same effect on the variable. For example, starting from 3, either count++; or ++count; leaves count holding 4. In these statements, the produced value is not saved.
Worked example
Save this program as UnaryDemo.java. It shows signs, logical NOT, increment, and decrement.
public class UnaryDemo {
public static void main(String[] args) {
int number = 4;
System.out.println(+number); // 4
System.out.println(-number); // -4
System.out.println(number); // 4
boolean ready = false;
System.out.println(!ready); // true
System.out.println(ready); // false
ready = !ready;
System.out.println(ready); // true
int count = 3;
int before = count++;
int after = ++count;
System.out.println(before); // 3
System.out.println(after); // 5
System.out.println(count); // 5
int stock = 3;
int previous = stock--;
int current = --stock;
System.out.println(previous); // 3
System.out.println(current); // 1
System.out.println(stock); // 1
}
}Compile and run it from the folder containing the file:
javac UnaryDemo.java
java UnaryDemoThe comments show the expected output. In the first part, -number produces -4, but number still holds 4. Likewise, !ready produces true without changing ready. The assignment ready = !ready then stores true in ready.
Now follow count one step at a time:
countstarts at3.count++produces the old value,3, which is stored inbefore. It also changescountto4.++countchangescountto5and produces that new value, which is stored inafter.
The stock example follows the same pattern in the other direction. Postfix stock-- produces 3 and leaves stock at 2. Prefix --stock then changes stock to 1 and produces 1.
Common mistakes
Expecting a sign or `!` to update a variable
Unary minus and logical NOT produce values; they do not update their operands. If number holds 4, evaluating -number does not make number hold -4.
Increment and decrement are different: number++ and number-- do update number. When you read code, check which kind of operator it uses.
Forgetting that postfix produces the old value
If count holds 3, int saved = count++; stores 3 in saved, not 4. The update to count still happens.
A useful way to remember the forms is:
- Prefix: update, then produce the new value.
- Postfix: produce the old value, then update.
Trying to update a value that cannot be stored
Increment and decrement need a variable that can be changed. 5++ is invalid: Java cannot store a new value in the literal 5. A literal is a value written directly in code.
A boolean variable also cannot use ++ or --. Use ! when you need the opposite boolean value.
Mixing updates into a hard-to-read expression
An update to a variable is a side effect: a change that happens as part of evaluating an expression. For example, count++ both produces a value and changes count.
Using one update in an assignment can be clear, as in the worked example. Expressions with several updates can be harder to follow. Prefer separate statements when that makes the code easier to read.
Practice
For each question, work out both the value produced by the expression and the value left in the variable before checking the answer.
Exercise
If count starts at 8, what are saved and count after int saved = count--;?
Show answer
saved is 8, and count is 7. Postfix decrement produces the old value.
Exercise
If count starts at 8, what are saved and count after int saved = --count;?
Show answer
Both are 7. Prefix decrement produces the new value.
Exercise
If ready starts as true, what does !ready produce, and what does ready hold afterward?
Show answer
The expression produces false, but ready still holds true. Logical NOT does not update the variable.