Java Unary Operators in a Nutshell

Nimesha Wijepala
3 min readDec 16, 2020

Introduction

Java has introduced various types of operators to perform different kinds of operations which we need to do in our implementations. Unary operators are one of them that requires only one operand to operate. We can use unary operators for several operations such as increment, decrement, negation, etc. Let’s see one by one.

Unary plus operator (+)

“+” operator can be used to indicate positive values. When “+” is written before any operand, that value becomes positive. However, any number has a positive value by default in java.

Unary minus operator (-)

“-” operator returns the negative value of the operand. As an example, -(+i) returns the negative value of the variable, i.

Increment operator (++)

We can use this operator before or after the operand. When “++” is written before the operand as a prefix, we can get the incremented value of the operand in the same executed line. It is like we implement to get the value of operand + 1.

But it is different when “++” is used after the operand as a postfix. Then we can get the incremented value of the operand after the executed line. It is like we implement to keep the value of operand + 1 in the memory for any upcoming operation. Let’s go through the example.

Output:++i in same executed line is 11
i in next executed line is 11
j++ in same executed line is 10
j in next executed line is 11

Decrement operator (--)

“--” operator can be used as the previous operator but the difference is that it decreases the value of the operand by one. We can implement both prefix and postfix as before in the same scenarios for decrements. See the below code snippet.

Output:--i in same executed line is 9
i in next executed line is 9
j-- in same executed line is 10
j in next executed line is 9

Logical complement operator (!)

We can simply use the “!” operator to invert any boolean value. If we use “!” before a boolean true it returns false, as well as “!” before boolean false returns true.

Unary bitwise complement operator (~)

“~” is a bit different than the previous operators because it returns a bitwise complement of the operand. In java documentation, it has been given as a bitwise operator. But, because “~” works with a single operand it is called the unary bitwise complement operator. When we write “~” before any operand, it inverts the binary value of the operand. See the example below.

Output:value of ~i is -11

Let’s check how the value of “~i” becomes -11. First, we have to convert decimal 10 to its binary value to understand the process. The binary value of decimal 10 is 1010. “~” inverts this binary value. Then it becomes 0101. You can say the decimal value of 0101 is 5. But wait! Do you know binary values of decimal 5 and -11 are equivalent.

In programming, the negative value of any number is taken through an easy process which is called getting 2’s complement. First, the decimal value is converted to its binary value and inverts it. That inverted value is called 1’s complement of the number. When 1 is added to the 1’s complement value, we can get the 2’s complement value and it is the negative value of the number. Let’s see the below image and get a clear idea.

Therefore it is easy to keep in mind that “~i” always returns -(i+1) as the output.

Conclusion

Well, among the various kinds of operators in java, unary operators take place to work with only one operand efficiently. Hope you got the point. Let’s learn programming with basic concepts.

--

--

Nimesha Wijepala

One who work in software industry as well as loves writing. Trying to combine both for sharing knowledge all over the world.