Java Unary Operators

‮gi.www‬iftidea.com

Java unary operators are operators that operate on a single operand. The following are the unary operators in Java:

  1. Unary plus: +
    The unary plus operator simply returns the value of the operand.
int a = 10;
int result = +a; // result is 10
  1. Unary minus: -
    The unary minus operator negates the value of the operand.
int a = 10;
int result = -a; // result is -10
  1. Increment: ++
    The increment operator adds 1 to the value of the operand.
int a = 10;
a++; // a is now 11
  1. Decrement: --
    The decrement operator subtracts 1 from the value of the operand.
int a = 10;
a--; // a is now 9
  1. Logical NOT: !
    The logical NOT operator returns the opposite boolean value of the operand.
boolean a = true;
boolean result = !a; // result is false