Java Unary Operators
gi.wwwiftidea.com
Java unary operators are operators that operate on a single operand. The following are the unary operators in Java:
- Unary plus:
+
The unary plus operator simply returns the value of the operand.
int a = 10; int result = +a; // result is 10
- Unary minus:
-
The unary minus operator negates the value of the operand.
int a = 10; int result = -a; // result is -10
- Increment:
++
The increment operator adds 1 to the value of the operand.
int a = 10; a++; // a is now 11
- Decrement:
--
The decrement operator subtracts 1 from the value of the operand.
int a = 10; a--; // a is now 9
- Logical NOT:
!
The logical NOT operator returns the opposite boolean value of the operand.
boolean a = true; boolean result = !a; // result is false