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 like the if else statement.
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 such as arithmetic operators, relational operators, and assignment operators that require two operands, unary operators work on only one value. You can read the basics in our Java operators tutorial.
💡 Key Point: Unary operators perform operations on a single variable or value.
Syntax
The basic syntax of unary operators is:
operator operand;
Example of Unary Operator in Java
public class Main
{
public static void main(String[] args)
{
int number = 10;
number++;
System.out.println(number);
}
}
Output
11
Explanation:
In the above example, the increment operator (++) increases the value of the variable by 1. If you are new to running programs, first read the Java Hello World program and Java environment setup pages.
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 TableJava Unary Operators Table
| Operator | Name | Description |
|---|---|---|
| + | Unary Plus | It represents a positive value |
| - | Unary Minus | It is used to change a positive value into negative |
| ++ | Increment | It is used to increase the value by 1 |
| -- | Decrement | It is used to decrease the value by 1 |
| ! | Logical NOT | It is used to reverse a boolean value |
| ~ | Bitwise Complement | It is used to invert the 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.
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.
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 is commonly used in the for loop and while loop.
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.
java
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.
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
int number = 10;
int result = --number;
System.out.println(result);
Output
9
Postfix Decrement 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 (see Java data types). It reverses the result of a condition. It is closely related to the other logical operators like && and ||.
If the value is true, NOT changes it to false, and if the value is false, it changes it to true.
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. To understand it better, also read about bitwise operators. It converts every 0 bit into 1 and every 1 bit into 0.
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
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, such as the for-each loop and do while loop.
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. Learn more in operator precedence and associativity.
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. Practice what you learned with the Java MCQ test, and continue with the complete Java Tutorial.