← Back to Home

Unary Operators

Unary operators in Java operate on a single operand. They are used for incrementing, decrementing, negation, and logical inversion and play a critical role in loops, expressions, and conditional logic.

This topic is frequently tested in interviews, especially pre vs post increment/decrement.

What Are Unary Operators?

  • Operate on only one operand
  • Perform simple but powerful operations
  • Often used in loops and expressions

List of Unary Operators in Java

Operator Name Description
+ Unary plus Indicates positive value
- Unary minus Negates a value
++ Increment Increases value by 1
-- Decrement Decreases value by 1
! Logical NOT Reverses boolean value

1. Unary Plus (+)

  • Indicates a positive value
  • Mostly redundant
int a = +10;  // same as 10
          

Why it matters: Rarely used explicitly, but valid syntax.

2. Unary Minus (-)

  • Converts a value to its negative form
int a = 10;
int b = -a;  // -10
          

3. Increment Operator (++)

Increases the value of a variable by 1.

There are two forms:

  1. Pre-increment (++x)
  2. Post-increment (x++)

a) Pre-Increment (++x)

  • Increments value before using it
int x = 5;
int y = ++x;
          

Result:

  • x = 6
  • y = 6

b) Post-Increment (x++)

  • Uses current value first, then increments
int x = 5;
int y = x++;
          

Result:

  • x = 6
  • y = 5

Real Interview Example

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

Evaluation:

  • a++ → 10 (a becomes 11)
  • ++a → 12

Output: 22

4. Decrement Operator (--)

Decreases the value of a variable by 1.

a) Pre-Decrement (--x)

int x = 5;
int y = --x;
          

Result:

  • x = 4
  • y = 4

b) Post-Decrement (x--)

int x = 5;
int y = x--;
          

Result:

  • x = 4
  • y = 5

5. Logical NOT Operator (!)

  • Works only on boolean values
  • Reverses the boolean result
boolean isActive = false;
System.out.println(!isActive); // true
          

Unary Operators with char

char ch = 'A';  // 65
ch++;
System.out.println(ch); // 'B'
          

Why it matters: Unary operators work on numeric value of char.

Important Rules & Restrictions

  • Increment/decrement works only on variables, not constants
  • Cannot apply ++ or -- to literals
10++;  // ❌ invalid
          

Unary Operators & Type Promotion

byte b = 10;
b++;   // ✅ allowed
          

But:

b = b + 1; // ❌ compilation error
          

Why: ++ handles implicit casting.

Common Beginner Mistakes

  • Confusing pre and post increment
  • Using ++ inside complex expressions
  • Applying unary operators on constants
  • Overusing increment operators, reducing readability

Interview-Ready Answers

Short Answer

Unary operators operate on a single operand and include increment, decrement, negation, and logical NOT.

Detailed Answer

Java unary operators such as ++, --, +, -, and ! work on one operand. Increment and decrement operators have pre and post forms, which differ in evaluation order and are commonly tested in interviews.

Key Takeaway

Unary operators are simple but tricky. Understanding pre vs post behavior is essential to avoid logical errors and succeed in interviews.