Java supports several types of operators including Arithmetic, Unary, Ternary, Bitwise, etc. All these operators serve a unique functionality and can be used as per the user’s preferences/requirements. Unary is a commonly used operator in Java that requires only one operand to perform any task. These tasks can be incrementing a value, complementing a value, reversing a boolean value, and so on.
This write-up will demonstrate the practical implementation of different unary operators in Java.
What is a Unary Operator in Java?
The unary operator is used with a single operand to perform a calculation. Java provides different types of unary operators that are listed in the following table along with the description:
Operator | Description | Syntax |
---|---|---|
Unary Plus(+) | It represents a positive value. | +var |
Unary Minus(-) | This operator represents a negative value. | -var |
Increment Operator(++) | It increments/increases a value by 1. | var++ or ++var |
Decrement Operator(–) | It decrements/decreases a value by 1. | var– or –var |
Logical Complement or “NOT” Operator(!) | It complements/reverses the value of a boolean variable. | !true or !false |
Bitwise Complement(~) | It retrieves the one’s complement of the given bit value (operand). | ~var |
Among the above-listed operators, increment and decrement are the most commonly used. For instance, these operators are used in loops, if-else conditional statements, shorthand expressions to increment/decrement variable’s value, array traversal, and many more.
How to Use a Java Unary Operator?
This section will discuss different types of Java unary operators using their basic syntax and practical examples.
1. Java Unary Plus(+) Operator
It is an optional operator and is used to denote positive values. The following code shows how a unary plus operator works in Java:
public class Examples {
public static void main(String args[]) {
int empId = +10;
int empId1 = 10;
System.out.println("Emp ID with Unary Plus: " + empId);
System.out.println("Emp ID without Unary Plus: " + empId1);
}
}
The output shows that the unary plus and the operand with no symbol retrieve the same result. This means an operand with no symbol is by default considered positive:
2. Java Unary Minus(-) Operator
The unary minus operator represents a negative value and is also used to convert a positive value to negative, as illustrated below:
public class Examples {
public static void main(String args[]) {
int empId = +10;
int empId1 = -100;
int empId2 = -(empId);
System.out.println("empId: " + empId);
System.out.println("empId1: " + empId1);
System.out.println("empId2: " + empId2);
}
}
The output shows that the unary minus operator converts a positive value to a negative:
3. Increment Operator(++)
It is one of the most frequently used unary operators that increments the operand/variable’s value by 1. It can be used/specified before or after the operand(variable). The one that is used before the variable is called pre-increment and the other one is known as post-increment. Let’s learn the use of pre and post-increment using the following example:
public class Examples {
public static void main(String args[]) {
int empId = 100;
System.out.println("Original empId: " + empId);
empId++;
System.out.println("Post-increment empId: " + empId);
++empId;
System.out.println("Pre-increment empId: " + empId);
}}
The following output shows that the variable’s value has been successfully incremented using the pre and post increment:
Post-increment Vs Pre-increment
The post-increment (var++) is first processed and then incremented while the pre-increment (++var) first increments the value and is then computed. This difference is illustrated in the below snippet:
public class Examples {
public static void main(String args[]) {
int empId = 100;
System.out.println("Original empId: " + empId);
System.out.println("Pre-increment empId: " + ++empId);
System.out.println("Post-increment empId: " + empId++);
System.out.println("Post-increment empId: " + empId);
}
}
From the output, you can observe that first, the post-increment prints the current value of “empId” (i.e., 101). While the next line prints the updated value of “empId” (i.e., 102):
4. Decrement Operator(–)
The decrement operator is also of two types: “pre-decrement” and “post-decrement”. It decrements the operand/variable’s value by 1. The following example shows how the pre and post-decrement operators work in Java:
public class Examples {
public static void main(String args[]) {
int empId = 100;
System.out.println("Original empId: " + empId);
System.out.println("Pre-decrement empId: " + --empId);
System.out.println("Post-decrement empId: " + empId--);
System.out.println("Post-decrement empId: " + empId);
}
}
The output confirms that both pre and post-decrement operators successfully decremented the value by 1:
5. Logical Complement or NOT Operator(!)
Logical complement or NOT is also a unary operator that is used on a single operand. It allows us to reverse a boolean value, i.e., true to false or false to true. The below code demonstrates the use of the logical complement operator in Java:
public class Examples {
public static void main(String args[]) {
boolean inputVal = false;
System.out.println("Original Value: " + inputVal);
System.out.println("Complemented Value: " + !inputVal);
}
}
The original value is “false” which is converted to “true” using the “!” operator:
6. Bitwise Complement(~)
The bitwise complement operator is used with a single operand and retrieves the 1’s complement of the given value/operand. It is used to compute the negation of an integer, perform bitwise operations, used for creating a mask for bit-level operations, etc.
This unary operator allows us to change/flip any bit from 0 to 1 or 1 to 0. For example, the binary value of “12” (in 8-bits) is “00001100” and its 1’s complement will be “11110011”, which is equal to “-13”. The following figure shows how the bitwise complement works in Java:
public class Examples {
public static void main(String args[]) {
int inputVal = 12;
System.out.println("Original Value: " + inputVal);
System.out.println("Bitwise Complement: " + ~inputVal);
}
}
The output demonstrates that the bitwise complement of “12” is “-13”:
That’s all about Java unary operators.
Conclusion
In a nutshell, an operator that is used with a single operand/variable is referred to as a unary operator in Java. Also, the unary operators enhance the coding efficiency, readability and are easy to use. Some commonly used unary operators include increment, decrement, bitwise complement, and logical complement. All these operators are discussed in this article, along with appropriate examples, to provide you with a better insight into Java unary operators.