What is Unary Operator?
Unary operators are quite different than any other operators in Java. Because this operator works with a single operand only. meaning they only require one value to perform the operation. These operators manipulate the value of the operand in various ways.
There are several unary operators in Java:
Unary Plus (+): This operator is used to indicate a positive value apparently. It doesn’t change the value of the operand.
Unary Minus (-): This operator reverses the value of the operand. If the operand is positive, it becomes negative, and if it’s negative, it becomes positive.
Increment (++): There are two forms: pre-increment (++operand) and post-increment (operand++).
Pre-increment (++operand): In the pre-increment form, the ++ operator is placed before the variable (operand). When used in an expression, the value of the operand is first incremented, and then the updated value is used in the expression.
Post-increment (operand++): In the post-increment form, the ++ operator is placed after the variable (operand). When used in an expression, the current value of the operand is used in the expression, and then the operand is incremented.
Decrement (–): Similar to the increment operator, it also has pre-decrement (–operand) and post-decrement (operand–) forms.
Pre-decrement (–operand): In the pre-decrement form, the — operator is placed before the variable (operand). When used in an expression, the value of the operand is first decremented, and then the updated value is used in the expression.
Post-decrement (operand–): In the post-decrement form, the — operator is placed after the variable (operand). When used in an expression, the current value of the operand is used in the expression, and then the operand is decremented.