In Java, unary arithmetic operators are used to increasing or decreasing the value of an operand.
Operator | Description |
+ | Unary plus operator; indicates positive value |
– | Unary minus operator; negates an expression |
++ | Increment operator; increments a value by 1 |
– – | Decrement operator; decrements a value by 1 |
! | Logical complement operator; inverts the value of a boolean |
1. Plus Operator(+)
It is used to represent positive values. Usually, we do not write the operator before the operand. Hence, it is optional.
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int number1 = 4;
System.out.println("Number 1: " + number1);
int result1 = +number1;
System.out.println("After unary plus operation result 1 is " + result1);
int number2 = -9;
System.out.println("Number 2: " + number2);
int result2 = +number2;
System.out.println("After unary plus operation result 2 is " + result2);
}
}
Output:
Number 1: 4
After unary plus operation result 1 is 4
Number 2: -9
After unary plus operation result 2 is -9
2. Minus Operator(-)
It is used to convert a positive value into a negative one.
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int number1 = 4;
System.out.println("Number 1: " + number1);
int result1 = -number1;
System.out.println("After unary minus operation result 1 is " + result1);
int number2 = -9;
System.out.println("Number 2: " + number2);
int result2 = -number2;
System.out.println("After unary minus operation result 2 is " + result2);
}
}
Output:
Number 1: 4
After unary minus operation result 1 is -4
Number 2: -9
After unary minus operation result 2 is 9
3. Increment Operator(++)
It is used to increment the value of an operand by one. The operator can be applied before or after an operand.
- Pre-increment Operator: If an increment operator is written before (prefix) the operand is known as pre-increment.
- Post-increment Operator: If an increment operator is written after (postfix) the operand is known as post-increment.
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int number = 4;
System.out.println("Number: " + number);
++number;
System.out.println("After Pre-increment operation number is " + number);
number++;
System.out.println("After Post-increment operation number is " + number);
}
}
Output:
Number: 4
After Pre-increment operation number is 5
After Post-increment operation number is 6
4. Decrement operator(– –)
It is used to decrement the value of an operand by 1. The operator can be applied before or after an operand.
- Pre-decrement Operator: If a decrement operator is written before (prefix) the operand is known as pre-decrement.
- Post-decrement Operator: If a decrement operator is written after (postfix) the operand is known as post-decrement.
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
int number = 4;
System.out.println("Number: " + number);
--number;
System.out.println("After Pre-decrement operation number is " + number);
number--;
System.out.println("After Post-decrement operation number is " + number);
}
}
Output:
Number: 4
After Pre-decrement operation number is 3
After Post-decrement operation number is 2
5. Logical Complement Operator(!)
A logical Complement Operator is used to reverse the value of a Boolean value. It means that if the value of an operand is true, then the complement of the operator will be false and vice-versa.
Example:
package octopuscodes.com.demo;
public class Demo {
public static void main(String[] args) {
boolean status1 = true;
System.out.println("Status1: " + status1);
status1 = !status1;
System.out.println("After logical complement operation status 1 is " + status1);
boolean status2 = false;
System.out.println("Status2: " + status2);
status2 = !status2;
System.out.println("After logical complement operation status 2 is " + status2);
}
}
Output:
Status1: true
After logical complement operation status 1 is false
Status2: false
After logical complement operation status 2 is true