Java Unary Operators
Unary operators in Java are operators that operate on a single operand. They perform various operations such as incrementing, decrementing, negating, or inverting the value of the operand. Unary operators are commonly used in programming for concise and effective code. Here’s a detailed explanation of the different types of unary operators in Java, along with examples.
Types of Unary Operators in Java
- Unary Plus Operator (
+
) - Unary Minus Operator (
-
) - Increment Operator (
++
) - Decrement Operator (
--
) - Logical NOT Operator (
!
)
1. Unary Plus Operator (+
)
The unary plus operator is used to indicate that a value is positive. It is typically not necessary to use it explicitly, as numeric values are positive by default.
Example:
public class UnaryPlusExample {
public static void main(String[] args) {
int a = 10;
int b = +a; // Unary plus, b is 10
System.out.println("Value of b: " + b); // Output: Value of b: 10
}
}
2. Unary Minus Operator (-
)
The unary minus operator negates the value of the operand, effectively changing its sign from positive to negative or vice versa.
Example:
public class UnaryMinusExample {
public static void main(String[] args) {
int a = 10;
int b = -a; // Unary minus, b is -10
System.out.println("Value of b: " + b); // Output: Value of b: -10
}
}
3. Increment Operator (++
)
The increment operator increases the value of its operand by 1. It can be used in two forms:
- Prefix (
++a
): Increments the value before using it. - Postfix (
a++
): Uses the current value and then increments it.
Example:
public class IncrementExample {
public static void main(String[] args) {
int a = 5;
int b = ++a; // Prefix increment, a becomes 6, b is 6
System.out.println("Value of a: " + a); // Output: Value of a: 6
System.out.println("Value of b: " + b); // Output: Value of b: 6
int c = 5;
int d = c++; // Postfix increment, c becomes 6, d is 5
System.out.println("Value of c: " + c); // Output: Value of c: 6
System.out.println("Value of d: " + d); // Output: Value of d: 5
}
}
4. Decrement Operator (--
)
The decrement operator decreases the value of its operand by 1. It can also be used in two forms:
- Prefix (
--a
): Decreases the value before using it. - Postfix (
a--
): Uses the current value and then decreases it.
Example:
public class DecrementExample {
public static void main(String[] args) {
int a = 5;
int b = --a; // Prefix decrement, a becomes 4, b is 4
System.out.println("Value of a: " + a); // Output: Value of a: 4
System.out.println("Value of b: " + b); // Output: Value of b: 4
int c = 5;
int d = c--; // Postfix decrement, c becomes 4, d is 5
System.out.println("Value of c: " + c); // Output: Value of c: 4
System.out.println("Value of d: " + d); // Output: Value of d: 5
}
}
5. Logical NOT Operator (!
)
The logical NOT operator inverts the boolean value of its operand. If the operand is true
, it returns false
, and vice versa.
Example:
public class LogicalNotExample {
public static void main(String[] args) {
boolean a = true;
boolean b = !a; // Inverts the value, b is false
System.out.println("Value of b: " + b); // Output: Value of b: false
}
}
Summary
Unary operators in Java are useful for performing operations on a single operand. They provide a compact way to manipulate numeric and boolean values. Understanding how to use these operators effectively can lead to more concise and readable code, improving your programming efficiency.