Java Unary Operators

In the last lesson you learned about Java logical operators. Some jobs have only one value to work on: counting up by one, flipping true into false, making a number negative. Java has a family of operators for exactly this, called unary operators.

πŸ€” Why one-operand operators?

Counting visitors? You could write count = count + 1 every time. But that is long, and you type the name twice:

  • A unary operator works on a single value, not two.
  • count++ does the same job as count = count + 1, just shorter.
  • You already met one: the ! that flips a boolean.

Here is the whole family this lesson covers.

  • + unary plus, keeps a number as it is.
  • - unary minus, makes a number negative.
  • ++ increment, adds one.
  • -- decrement, takes one away.
  • ! logical NOT, flips a boolean.
  • ~ bitwise complement, flips the bits (a quick look only).

βž• Unary plus and minus

The two easiest ones:

  • + in front of a number does nothing useful. It just says β€œthis number, as it is”.
  • - flips the sign. Positive becomes negative, negative becomes positive.

The code below shows both signs on a few numbers.

int profit = 50;
int loss = -30;
System.out.println(+profit);
System.out.println(-profit);
System.out.println(-loss);

Line by line:

  • +profit is just 50. The plus changed nothing.
  • -profit flips 50 into -50.
  • -loss flips -30 into 30, because loss was already negative.

Output

50
-50
30

The unary minus is the one you will actually reach for. Unary plus is rarely used, because a plain number is already positive.

βž•βž• Increment with ++

The increment operator ++ adds one to a variable. x++ is the same as x = x + 1, just shorter.

The code below increments a score a couple of times.

int score = 10;
score++;
System.out.println(score);
score++;
System.out.println(score);

Walk through it:

  • score starts at 10.
  • The first score++ bumps it to 11.
  • The second score++ bumps it to 12.

Output

11
12

The variable climbs by one each time. The decrement operator is the mirror image.

βž–βž– Decrement with β€”

The decrement operator -- is the opposite of ++. It takes one away. So x-- means x = x - 1. Perfect for counting down lives in a game.

The code below removes a life twice.

int lives = 3;
lives--;
System.out.println(lives);
lives--;
System.out.println(lives);

Read it through:

  • lives starts at 3.
  • The first lives-- drops it to 2.
  • The second lives-- drops it to 1.

Output

2
1

On their own line, ++ and -- are easy. But a twist shows up when you use them inside a bigger expression.

πŸ”€ Prefix vs postfix: the big one

You can write ++ in two places, and both add one to x. The variable ends up the same. What changes is the value the expression hands back.

  • Prefix ++x: ++ comes before. It adds one first, then gives back the new value.
  • Postfix x++: ++ comes after. It gives back the old value first, then adds one.

The code below uses postfix. Watch what gets stored in b.

int a = 5;
int b = a++;
System.out.println("a = " + a);
System.out.println("b = " + b);

The heart of the lesson:

  • a starts at 5.
  • int b = a++ is postfix, so Java hands back the old 5 and stores it in b.
  • Then a gets its plus one, becoming 6.
  • Result: b is 5, but a is 6.

Output

a = 6
b = 5

Now the same code with prefix. One character moves, and b changes.

int a = 5;
int b = ++a;
System.out.println("a = " + a);
System.out.println("b = " + b);

Step through the difference:

  • a starts at 5.
  • int b = ++a is prefix, so Java adds one first, making a become 6.
  • That new 6 is handed back and stored in b.
  • Result: b is 6, and a is 6 as well.

Output

a = 6
b = 6

See the pattern? In both cases a ended at 6. Only b changed:

  • Postfix gave b the old 5: it uses the value, then changes it.
  • Prefix gave b the new 6: it changes the value, then uses it.

Tip

A memory trick. Read the symbols left to right. In a++, the a comes first, so the value of a is used first. In ++a, the ++ comes first, so the adding happens first.

πŸ” Increment and decrement in loops

The most common home for ++ is a loop. A for loop counts through a range, and ++ moves the count forward each round. The code below prints one to five.

for (int i = 1; i <= 5; i++) {
System.out.println(i);
}

Each time around:

  • i starts at 1.
  • After the body runs, i++ adds one to i.
  • The loop keeps going while i <= 5 is true.

Output

1
2
3
4
5

Does i++ or ++i matter here? For a normal for loop, no:

  • Nothing stores the result, so the returned value is thrown away.
  • The loop only cares that i goes up by one, and both forms do that.
  • for (int i = 1; i <= 5; ++i) prints the same thing.
  • Prefix-versus-postfix only matters when you use the returned value, like int b = a++ earlier.

A countdown loop is just as common. Here -- walks a number down to one.

for (int count = 3; count >= 1; count--) {
System.out.println(count + "...");
}
System.out.println("Go!");

count starts at 3, drops by one each round until it reaches one, then prints the final message.

Output

3...
2...
1...
Go!

❗ The logical NOT (!)

You met ! in the logical operators lesson, but it belongs here too because it is unary.

  • The logical NOT takes one boolean and flips it.
  • true becomes false, and false becomes true.
  • It works only on booleans, not numbers.

The code below flips a couple of boolean values.

boolean isOnline = true;
System.out.println(!isOnline);
boolean isEmpty = false;
System.out.println(!isEmpty);

Read it through:

  • isOnline is true, so !isOnline flips it to false.
  • isEmpty is false, so !isEmpty flips it to true.

Output

false
true

You will use ! a lot inside if statements when you care about the opposite of a check. if (!isLoggedIn) reads as β€œif the user is not logged in”.

πŸ”§ The bitwise complement (~)

The last unary operator is ~, the bitwise complement:

  • It works on the individual bits of a whole number.
  • It flips every bit: each 0 becomes 1, each 1 becomes 0.
  • For everyday integers, the short rule is ~n equals -n - 1.

The code below shows that rule on a small number.

int n = 5;
System.out.println(~n);

Here n is 5, so ~n works out to -5 - 1, which is -6.

Output

-6

Do not worry if this feels strange. You will rarely touch ~ in normal app code. The full story of bits lives in the next lesson.

⚠️ Common Mistakes

The ++ and -- operators cause more confusion than any other beginner topic. Watch for these slip-ups.

Mixing up prefix and postfix when the value is used. Storing the result, the position changes what gets stored.

// ❌ Avoid: expecting b to be 6 here. Postfix gives the OLD value, so b is 5
// int a = 5;
// int b = a++; // b becomes 5, not 6
// βœ… Good: use prefix when you want the NEW value
int a = 5;
int b = ++a; // b becomes 6
System.out.println(b);

Using ++ inside a complex expression. Mixing ++ into a long line makes the result hard to predict and hard to read. Pull it onto its own line.

// ❌ Avoid: this is confusing and easy to get wrong
// int x = 5;
// int result = x++ + ++x;
// βœ… Good: change x clearly, then use it
int x = 5;
x++;
int result = x + x;
System.out.println(result);

Changing the same variable twice in one statement. Writing something like i = i++ + 1 looks clever but the result surprises almost everyone. Keep one change per statement.

// ❌ Avoid: two changes to i in one line is a classic trap
// int i = 5;
// i = i++; // i stays 5, which is almost never what you wanted
// βœ… Good: one clear action
int i = 5;
i++; // i becomes 6
System.out.println(i);

Trying to increment a literal number. ++ needs a variable to store the new value back into. 5++ makes no sense, and Java will refuse to compile it.

// ❌ Avoid: you cannot increment a plain number
// System.out.println(5++);
// βœ… Good: increment a variable
int value = 5;
value++;
System.out.println(value);

βœ… Best Practices

A few habits keep increment and decrement code clean and safe.

  • Keep ++ and -- on their own line when you can. count++; on its own is crystal clear. Buried inside a big expression, it is a guessing game.
  • Prefer postfix i++ in plain for loops. It is the common style, and the returned value is ignored there anyway, so it reads naturally.
  • Reach for prefix ++x only when you really want the new value. And when you do, add a short comment so the next reader knows it was on purpose.
  • Never change the same variable twice in one statement. One change per line keeps the result obvious.
  • Name booleans for the positive case. Then !isReady reads cleanly, instead of a confusing double negative like !isNotReady.

🧩 What You’ve Learned

Nice work. The prefix-versus-postfix idea trips up a lot of people, and you just worked through it.

  • βœ… A unary operator works on a single value, not two.
  • βœ… Unary - flips the sign of a number; unary + leaves it unchanged.
  • βœ… ++ adds one and -- takes one away.
  • βœ… Postfix x++ gives the old value first, then adds one.
  • βœ… Prefix ++x adds one first, then gives the new value.
  • βœ… In a normal for loop, i++ and ++i behave the same, because the result is not used.
  • βœ… ! flips a boolean, and ~ flips the bits of a number.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does b hold after int a = 5; int b = a++;?

    Why: Postfix a++ hands back the OLD value of a, which is 5, so b is 5. After that, a becomes 6.

  2. 2

    What does b hold after int a = 5; int b = ++a;?

    Why: Prefix ++a adds one to a first, making it 6, and then returns that new value, so b is 6.

  3. 3

    In a plain for loop like for (int i = 0; i < 5; i++), does using ++i instead change the result?

    Why: The loop only needs i to go up by one. The returned value is ignored, so i++ and ++i behave the same here.

  4. 4

    What does the ! operator do?

    Why: The logical NOT (!) works on a single boolean and reverses it: true becomes false and false becomes true.

πŸš€ What’s Next?

You met the bitwise complement ~ briefly in this lesson. There is a whole set of operators that work on the individual bits of a number, and they unlock some clever, fast tricks. Let’s dig into them next.

Java Bitwise Operators

Share & Connect