Unary Operators in Java
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.
Syntax of Unary Operators in Java
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);
}
}
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.
int number = 10;
int result = +number;
System.out.println(result);
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);
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.
int number = 5;
int result = ++number;
System.out.println(result);
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);
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);
Postfix Decrement Example
int number = 10;
int result = number--;
System.out.println(result);
System.out.println(number);
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.
boolean status = true;
System.out.println(!status);
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.
int number = 5;
System.out.println(~number);
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);
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.