A Unary Operator in Java is a special type of operator that performs an operation on a single operand. It is commonly used to modify a value, reverse its sign, or evaluate logical expressions, making code more concise and efficient.
- Unary operators require only one operand to perform an operation.
- They are frequently used for value manipulation and condition evaluation.
- Unary operators can increase code readability by reducing the need for additional statements.
Types of Unary Operators in Java
1. Unary Minus Operator (-)
The unary minus operator changes the sign of its operand. It converts a positive value into a negative value and vice versa.
- Negates the value of the operand.
- Commonly used in arithmetic expressions and calculations.
Syntax:
-operand
// Importing required classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a custom variable
int n1 = 20;
// Printing the above variable
System.out.println("Number = " + n1);
// Performing unary operation
n1 = -n1;
// Printing the above result number
// after unary operation
System.out.println("Result = " + n1);
}
}
Output
Number = 20 Result = -20
2. Logical NOT Operator (!)
The logical NOT operator reverses a boolean value. If the operand is true, it becomes false, and vice versa.
Syntax:
!(operand)
// Importing required classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Initializing variables
boolean cond = true;
int a = 10, b = 1;
// Displaying values stored in above variables
System.out.println("Cond is: " + cond);
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
// Displaying values stored in above variables
// after applying unary NOT operator
System.out.println("Now cond is: " + !cond);
System.out.println("!(a < b) = " + !(a < b));
System.out.println("!(a > b) = " + !(a > b));
}
}
Output
Cond is: true Var1 = 10 Var2 = 1 Now cond is: false !(a < b) = true !(a > b) = false
3. Increment Operator (++)
The increment operator increases the value of a variable by 1. It is available in both prefix and postfix forms. It can be used in two separate ways:
1: Post-increment operator
When placed after the variable name, the value of the operand is incremented but the previous value is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.
Syntax:
num++
2: Pre-increment operator
When placed before the variable name, the operand's value is incremented instantly.
Syntax:
++num
4. Decrement Operator (--)
The decrement operator decreases the value of a variable by 1. Like increment, it has both prefix and postfix forms. It can be used in two separate ways:
1: Post-decrement operator
When placed after the variable name, the value of the operand is decremented but the previous values is retained temporarily until the execution of this statement and it gets updated before the execution of the next statement.
Syntax:
num--
2: Pre-decrement operator
When placed before the variable name, the operand's value is decremented instantly.
Syntax:
--num
5. Bitwise Complement Operator (~)
The bitwise complement operator inverts every bit of an integer value. All 0s become 1s, and all 1s become 0s.
Syntax:
~(operand)
// Importing required classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a variable
int n1 = 6, n2 = -2;
// Printing numbers on console
System.out.println("First Number = " + n1);
System.out.println("Second Number = " + n2);
// Printing numbers on console after
// performing bitwise complement
System.out.println(
n1 + "'s bitwise complement = " + ~n1);
System.out.println(
n2 + "'s bitwise complement = " + ~n2);
}
}
Output
First Number = 6 Second Number = -2 6's bitwise complement = -7 -2's bitwise complement = 1
Example: Program in Java that implements all basic unary operators for user input:
import java.util.Scanner;
public class UnaryOperators {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Uncomment this block for user input
// System.out.print("Enter a number: ");
// int num = sc.nextInt();
// Initialize num
int num = 10;
// Unary plus
int result = +num;
System.out.println("The value of result after unary plus is: " + result);
// Unary minus
result = -num;
System.out.println("The value of result after unary minus is: " + result);
// Pre-increment
result = ++num;
System.out.println("The value of result after pre-increment is: " + result);
// Post-increment
result = num++;
System.out.println("The value of result after post-increment is: " + result);
// Pre-decrement
result = --num;
System.out.println("The value of result after pre-decrement is: " + result);
// Post-decrement
result = num--;
System.out.println("The value of result after post-decrement is: " + result);
// Close the scanner
sc.close();
}
}
Output
The value of result after unary plus is: 10 The value of result after unary minus is: -10 The value of result after pre-increment is: 11 The value of result after post-increment is: 11 The value of re...
Explanation: This program demonstrates the use of basic unary operators in Java with user input. It uses the Scanner class to read a number from the user and then applies various unary operators such as unary plus (+), unary minus (-), pre/post-increment (++), and pre/post-decrement (--). After each operation, the result is displayed using System.out.println(), helping users understand how unary operators modify a variable's value.
Advantages
- Concise and Easy to Use: Unary operators are simple to use and require only one operand. They are easy to understand and make code more readable and concise.
- Faster than Other Operators: Unary operators are faster than other operators as they only require one operand. This makes them ideal for operations that need to be performed quickly, such as incrementing a counter.
- Pre- and Post-Increment/Decrement: Unary operators provide both pre- and post-increment and decrement options, which makes them useful for a variety of use cases. For example, the pre-increment operator can be used to increment the value of a variable before using it in an expression, while the post-increment operator can be used to increment the value of a variable after using it in an expression.
- Modifying Primitive Types: Unary operators can be used to modify the value of primitive types such as int, long, float, double, etc.