Home Java Unary Operators

Unary Operators in Java

Beginner ⏱ 8 min read Updated: Jun 2026

Unary operators in Java are operators that work with only one operand. They are used to perform operations such as increasing or decreasing a value, changing the sign of a number, and reversing boolean values.

Unary operators are simple but very useful in Java programming, especially when working with loops, counters, calculations, and conditional statements.

What are Unary Operators in Java?

A unary operator is an operator that requires only a single operand to perform an operation.

Unlike binary operators that require two operands, unary operators work on only one value.

💡
Key Point: Unary operators perform operations on a single variable or value.

Syntax of Unary Operators in Java

The basic syntax of unary operators is:

Syntax
operator operand;

Example of Unary Operator in Java

Example
public class Main {
    public static void main(String[] args) {
        int number = 10; number++;
        System.out.println(number);
    }
}
Output
11

In the above example, the increment operator (++) increases the value of the variable by 1.

Types of Unary Operators in Java

Java provides different types of unary operators to perform various operations on a single operand.

  • Unary Plus Operator (+)
  • Unary Minus Operator (-)
  • Increment Operator (++)
  • Decrement Operator (--)
  • Logical NOT Operator (!)
  • Bitwise Complement Operator (~)

Java Unary Operators Table

Operator Name Description
+ Unary Plus Represents a positive value
- Unary Minus Changes positive value into negative
++ Increment Increases value by 1
-- Decrement Decreases value by 1
! Logical NOT Reverses boolean value
~ Bitwise Complement Inverts bits of a number

Unary Plus Operator (+) in Java

The unary plus operator (+) is used to indicate that a value is positive. It does not change the value of the operand.

Example
int  number = 10;
int result = +number; 
System.out.println(result);
Output
10

Unary Minus Operator (-) in Java

The unary minus operator (-) changes the sign of a value. Positive values become negative and negative values become positive.

Example
int number =  10;
int result = -number; 
System.out.println(result);
Output
-10

Increment Operator (++) in Java

The increment operator (++) increases the value of a variable by 1.

It can be used in two ways:

  • Prefix Increment (++variable)
  • Postfix Increment (variable++)

Prefix Increment Operator

In prefix increment, the value is increased first and then used in the expression.

Example
int number = 5;
int  result = ++number;
System.out.println(result);
Output
6

Postfix Increment Operator

In postfix increment, the current value is used first and then increased by 1.

Example
int number = 5;
int result = number++;
System.out.println(result);
System.out.println(number);
Output
5
6

Decrement Operator (--) in Java

The decrement operator (--) decreases the value of a variable by 1.

Similar to increment, decrement also has two forms:

  • Prefix Decrement (--variable)
  • Postfix Decrement (variable--)

Prefix Decrement Example

Example
int number = 10;
int result = --number;
System.out.println(result);
Output
9

Postfix Decrement Example

Example
int  number = 10;
int result = number--;
System.out.println(result);
System.out.println(number);
Output
10
9

Logical NOT Operator (!) in Java

The logical NOT operator (!) is used with boolean values. It reverses the result of a condition.

If the value is true, NOT changes it to false, and if the value is false, it changes it to true.

Example
boolean status =  true;
System.out.println(!status);
Output
false

Bitwise Complement Operator (~) in Java

The bitwise complement operator (~) is a unary operator that reverses all bits of an integer value.

It converts every 0 bit into 1 and every 1 bit into 0.

Example
int number = 5;
System.out.println(~number);
Output
-6

Difference Between Prefix and Postfix Operators

Prefix Operator Postfix Operator
Operator is written before the variable. Operator is written after the variable.
Value is changed first. Value is used first.
Example: ++number Example: number++
Commonly used when updated value is required immediately. Commonly used when original value is required first.

Unary Operators Example with Multiple Operations

Example
int number = 10;
System.out.println(++number);
System.out.println(--number);
Output
11
10

Advantages of Unary Operators in Java

Simple Syntax

Unary operators provide a short and simple way to perform operations on variables.

Loop Control

Increment and decrement operators are widely used in loops and counters.

Efficient Code

They reduce the amount of code required for common operations.

Easy Calculation

Unary operators make mathematical and logical operations easier.

Common Mistakes While Using Unary Operators

  • Confusing prefix and postfix increment behavior.
  • Using increment or decrement operators with constant values.
  • Applying logical NOT operator on non-boolean values.
  • Using multiple unary operators without understanding evaluation order.

Summary

Unary operators in Java operate on a single operand. They are used for changing values, increasing or decreasing numbers, and performing logical operations.

Java provides several unary operators such as unary plus (+), unary minus (-), increment (++), decrement (--), logical NOT (!), and bitwise complement (~).

Understanding unary operators is important for writing efficient Java programs, especially when working with loops, conditions, and calculations.