Unary Minus (-)
This operator converts a positive number into a negative number. It is equivalent to multiplying with -1. For example -5, -x.
Syntax:
-(operand)
Example:
public class Main{
public static void main(String args[]){
int firstVariable= 5;
// the second variable is assigned the value using unary minus
int secondVariable= -5;
int thirdVariable= - firstVariable;
System.out.println(firstVariable);
System.out.println(secondVariable);
System.out.println(thirdVariable);
//the value of the second variable and third variable is -5
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
5
-5
-5

You can also try this code with Online Java Compiler
Run Code
Increment Operator(++)
The increment operator is used to increase the value of an operand by one. It is represented by ++. Based on the way the value is increased, the increment operator is of two types.
Pre-Increment Operator
Whenever ++ is written before a variable or an operand, it is known as a pre-increment operation. In the pre-increment operation, first, the value of the operand is increased by one, and then it is taken for any computation or arithmetical operation.
Syntax:
++(operand)
Examples: ++a, ++5.
Example:
class CodingNinjas{
public static void main(String args[]){
int val= 20;
//pre-increment, equivalent to val=val+1
++val;
System.out.println("After pre-increment the value is: "+val);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
After pre-increment the value is: 21

You can also try this code with Online Java Compiler
Run Code
Explanation:
- First, the value of the variable val is assigned as 20.
- When we write ++val, it is equivalent to writing val= val+1.
- The value of val increments by one and becomes 21.
- Finally, the output is 21.
Post-Increment Operator
Whenever ++ is written after a variable or an operand, it is known as a post-increment operation. In the post-increment operation, the value of the operand is first used for computation or any other operation and then incremented by one.
Syntax:
(operand)++
Example: a++, 5++.
Example:
class CodingNinjas{
public static void main(String args[]){
int val= 20;
//post-increment
//first the value of val is used by temp
//and then incremented by one
int temp=val++;
//the temp holds the value 20
System.out.println("Value of temp: "+temp);
//value of val is 21
System.out.println("Value of val after post-increment:"+val);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Value of temp: 20
Value of val after post-increment:21

You can also try this code with Online Java Compiler
Run Code
Explanation:
- The variable val is assigned a value 20.
- When we post-increment val at that moment, the value of val is 20 only. It is stored in temp.
- After that val is incremented by one.
The pre-increment operator first increments the value of the operand and then uses the operand for computation, whereas the post-increment operator first uses the operand for computation and then increments its value by one.
You can also read about Java Destructor and Hashcode Method in Java.
Decrement Operator(--)
The decrement operator is used to decrease the value of an operand by one. It is represented by --. Based on the way the value is decreased, the increment operator is of two types.
Pre-Decrement Operator
Whenever -- is written before a variable or an operand, it is known as a pre-decrement operation. In the pre-decrement operation, first, the value of the operand is decreased by one, and then it is taken for any computation or arithmetical operation.
Syntax:
--(operand)
Example: --a, --6
Example:
class CodingNinjas{
public static void main(String args[]){
int val= 20;
//pre-decrement val
//equivalent to val= val-1
--val;
System.out.println("The value of val after pre-decrement is: "+val);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
The value of val after pre-decrement is: 19

You can also try this code with Online Java Compiler
Run Code
Explanation:
- The variable val is assigned a value 20.
- The pre-decrement operation is performed on val by using --val.
- The value of val decrements by 1. Now the value of val is 19.
- Finally, 19 is printed.
Post-Decrement Operator
Whenever -- is written after a variable or an operand, it is known as a post-decrement operation. In the post-decrement operation, the value of the operand is first used for computation or any other operation and then decremented by one.
Syntax:
(operand)--
Example: a--., 5--
Example:
class CodingNinjas{
public static void main(String args[]){
int val= 20;
//prints 20
System.out.println(val--);
//prints 19
System.out.println(val);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
20
19

You can also try this code with Online Java Compiler
Run Code
Explanation:
- The variable val is assigned a value equal to 20.
- For the first print statement, the value of val remains unchanged since it is a post-decrement operation.
- For the second print statement, the value of val decrements by one, and the new value of val is 19.
Logical Complement Operator
The logical complement operator reverses the value of an operand. If the value of the operand is true, then by using the logical complement operator with the operand, we change its value to false and vice-versa.
Syntax:
!(operand)
Example: !true, !false
Example:
class CodingNinjas{
public static void main(String args[]){
boolean val1= true;
boolean val2= false;
//val1 is true, therefore !val1= false
//prints false
System.out.println(!val1);
//val2 is false, therefore !val2= true
//prints true
System.out.println(!val2);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
false
true

You can also try this code with Online Java Compiler
Run Code
Try it on online java compiler.
Explanation:
- The value of val1 is true. Therefore the complement of val1 will be false.
- The value of val2 is false. Therefore the complement of val2 will be true.
Must Read Conditional Statements in Java
FAQs
1.What are unary operators in Java?
The unary operator in Java is an operator that is used with only one operand.
2. How many types of unary operators are there?
There are five types of unary operators. They are plus operator, minus operator, increment operator, decrement operator, and logical complement operator.
3. What is the difference between pre-increment and post-increment?
In pre-increment operation, the value of the operand is first incremented by one and then used for any other purpose, but in post-increment, first the value of the operand is used and then incremented by one.
4. What is the use of a logical complement operator?
It is used to reverse the value of the operand. It changes the true value to false and vice-versa.
Key Takeaways
In this article, we have extensively discussed unary operators in Java and its various code implementations.
- The unary operator in Java is an operator that is used with only one operand.
- There are five types of unary operators. They are plus operator, minus operator, increment operator, decrement operator, and logical complement operator.
Check out this problem - First Missing Positive
We hope that this blog has helped you enhance your knowledge regarding unary operators and if you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. Happy Coding!