Introduction to Unary Operators in Java
In Java, Unary operators operates on only one operand. It consist of arithmetic operators (+ and -), logical operator (!) along with increment and decrement operators.
Java supports the following Unary operators.
- Unary Plus Operator(+)
- Unary Minus Operator(-)
- Logical NOT Operator (!)
- Increment Operator (++)
- Decrement Operator (--)
Syntax to use Unary Operators in Java
The generalized syntax to use all the Unary operator is as shown below:
unary_operator operand
Different types of Unary Operators in Java
1. Unary Plus Operator (+)
In Java, Unary + operator is used to indicates the positive value. However, numbers are positive without this also. It follows the following rules of mathematics.
- Minus and Minus makes Plus
- Minus and Plus makes Minus
- Plus and Minus makes Minus
- Plus and Plus makes Plus
The statement below demonstrates the syntax of unary "+
" operator.
result = + operand
Example :
public class UnaryDemo
{
public static void main(String[] args)
{
// Initializing
int a=8,b=-2;
int result;
// Using Unary + operator
System.out.println("Value of a is "+a);
result=+a;
System.out.println("After unary plus operation result is "+result);
// Using Unary + operator
System.out.println("Value of b is "+b);
result=+b;
System.out.println("After unary plus operation result is "+result);
}
}
Output
Value of a is 8
After unary plus operation result is 8
Value of b is -2
After unary plus operation result is -2
2. Unary Minus Operator (-)
In Java, Unary - operator is used to convert a positive value into a negative value. It also follows the following rules of mathematics.
- Minus and Minus makes Plus
- Minus and Plus makes Minus
- Plus and Minus makes Minus
- Plus and Plus makes Plus
The statement below demonstrates the syntax of unary "-
" operator.
result = - operand
Example :
public class UnaryDemo
{
public static void main(String[] args)
{
// Initializing
int a=8,b=-2;
int result;
// Using Unary - operator
System.out.println("Value of a is "+a);
result=-a;
System.out.println("After unary minus operation result is "+result);
// Using Unary - operator
System.out.println("Value of b is "+b);
result=-b;
System.out.println("After unary minus operation result is "+result);
}
}
Output
Value of a is 8
After unary minus operation result is -8
Value of b is -2
After unary minus operation result is 2
3. Logical NOT Operator (!)
In Java, Logical NOT operator "!
" is a unary operator that is used to reverses the logical state of its condition. In other words, if a result of the condition is true then Logical NOT operator will make it as false. Similarly, if the condition is false
, it will make it true
.
The statement below demonstrates the "!" operator.
result = ! condition
Example :
public class LogicalDemo
{
public static void main(String[] args)
{
// Initializing
int a=10;
int b=10;
// Using logical NOT
if(!(a<50))
System.out.println("Value of a is greater than 50");
else
System.out.println("Value of a is less than 50");
System.out.println("Showing reverse result of a==b "+!(a==b));
}
}
Output
Value of a is less than 50
Showing reverse result of a==b false
4. Increment Operator (++)
In Java, Increment operator "++" is a unary operator that increments the value of operand by 1. We can use this in two different ways referred as Pre-Increment and Post-Increment. However, there is no difference between both the approach, if this statement is not used in an expression. Increment operator is also widely used in looping statements.
Post-Increment : When operator is placed after an operand, the value of operand is incremented after all the computation is done. In other words, it computes with the old value of an operand and thereby incrementing the value before executing the next statement in code.
Pre-Increment : When operator is placed before an operand, the value of operand is incremented before all the computation is done. In other words, it increments the value of an operand and then use the new value in computing an expression.
The statement below demonstrates the syntax of post and pre increment "++" operator.
operand ++
++operator
Example :
public class UnaryDemo
{
public static void main(String[] args)
{
// Initializing
int a=10;
int b,c;
int i;
// Increment operator without any expression
++a;
System.out.println("Value of a is "+a);
// Post Increment - assigns the value 10 to b and then increments a
b=a++;
System.out.println("Value of b is "+b+" Value of a is "+a);
// Pre Increment - increments a by 1 and then assigns value to c
c=++a;
System.out.println("Value of c is "+c+" Value of a is "+a);
// Using in for loop
for(i=0;i<3;i++)
System.out.println("Number is "+i);
}
}
Output
Value of a is 11
Value of b is 11 Value of a is 12
Value of c is 13 Value of a is 13
Number is 0
Number is 1
Number is 2
5. Decrement Operator (--)
In Java, Decrement operator "--" is a unary operator that decrements the value of operand by 1. We can use this in two different ways referred as Pre-decrement and Post-decrement. However, there is no difference between both the approach, if this statement is not used in an expression.
Post-decrement : When operator is placed after an operand, the value of operand is decremented after all the computation is done. In other words, it computes with the old value of an operand and thereby decrementing the value before executing the next statement in code.
Pre-decrement : When operator is placed before an operand, the value of operand is decremented before all the computation is done. In other words, it decrements the value of an operand and then use the new value in computing an expression.
The statement below demonstrates the syntax of post and pre decrement "--" operator.
operand--
--operator
Example :
public class UnaryDemo
{
public static void main(String[] args)
{
// Initializing
int a=10;
int b,c;
int i;
// Decrement operator without any expression
--a;
System.out.println("Value of a is "+a);
// Post Decrement - assigns the value 10 to b and then decrements a
b=a--;
System.out.println("Value of b is "+b+" Value of a is "+a);
// Pre Decrement - decrements a by 1 and then assigns value to c
c=--a;
System.out.println("Value of c is "+c+" Value of a is "+a);
// Using in for loop
for(i=3;i>0;i--)
System.out.println("Number is "+i);
}
}
Output
Value of a is 9
Value of b is 9 Value of a is 8
Value of c is 7 Value of a is 7
Number is 3
Number is 2
Number is 1
Summary
The knowledge of Unary operators is a key to start building the logic in Java. The Unary operators are extensively used in the looping. In this tutorial, we covered all Unary operators supported in Java. We learned in detail about the syntax and how we can use this operators with examples. All in all, this tutorial, covers everything that you need to know in order to have a solid command over Unary operators in Java.
References
Operators
More about Java Operators