What are Unary Operators in Java?

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
By Tarun Luthra
Free
star5
Enrolled: 1000
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
Tarun Luthra
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Operators in Java are an essential part of any programming language. They allow developers to perform various calculations and operations, such as arithmetic, logical, and relational. Java, like many other programming languages, offers a wide range of operators that can be used to execute various tasks and make programming more efficient.

Unary operators are a type of operator that only require one operand to perform an operation. These operations can include incrementing or decrementing a value, reversing a boolean value, or other types of calculations. Examples of unary operators include the increment operator (++), decrement operator (--), and negation operator (!). It is important to note that Unary operators can be applied to different types like arithmetic, logic, and others.

Types of Unary operators are:

Types of Unary operator

Types of Unary Operators in Java

Let's see different types of unary operators in Java :

SymbolOperator NameDescription
-Unary MinusIt is used to denote a negative value.
+Unary PlusIt is used to denote a positive value.
- -Decrement OperatorDecrements value by 1
++Increment OperatorIncrements value by 1
!Logical Complement OperatorReverse the value of boolean variable

Unary Plus

It is used to represent a positive operand. In this, the use of the operator is optional that is we do not have to write the operator before operand.

Syntax:

Example:

Unary Minus

It converts a positive value into a negative value and vice versa.

Syntax:

Example:

Explanation:

  • In the code above, the value of x1 was initially 12 before the unary minus(-) operator was applied to it as a result, it changed to -12 since the unary operator negated its value.

Output:

Increment Operator

The increment operator in java is used to increase the value of the variable(operand) by one(1). The symbol for the operator is two plus signs (++). The operator can be applied before or after the operand.

In java increment operator is used in two forms:

  1. Pre-increment operator (Prefix)
  2. Post-increment operator (Postfix)

Pre-increment Operator:

Pre-increment operator is defind as when the increment operator(++) is written before a variable then it is known as Pre-increment operator or Prefix. The Pre-increment operator works in such a way that, first the value is incremented by one(1), then returns the new incremented value.

Syntax:

Example:

Explanation:

  • In the very first statement, a value 12 has been assigned to a.
  • Then, in the next statement, it performs two actions.
  • It increments the value of a to 13.
  • Then assign the new incremented value to b.

Output:

Post-increment Operator:

Post-increment operator is defined as when the increment operator(++) is written after a variable then it is known as Post-increment operator or Postfix. The Post-increment operator works in such a way that, initially, the operator returns the value of the operand, and finally, the value is incremented by one(1).

Syntax:

Example:

Explanation:

  • In the above code, we can see that, first, the post-increment operator assigns the value in the left-hand side variable b.
  • Then the operator increments the value of a by 1.

Output:

Note: Both i += 1 and i++ evaluate the same result but the only difference between them is that i++ uses the increment operator(unary operator) while i+=1 uses the assignment operator.

Decrement Operator

Decrement operator in java is used to decrement the value of operand by one(1). The symbol for the operator is two minus signs (--). Same as of increment operator it can also be applied before and after an operand.

In java decrement operator is used in two forms:

  1. Pre-decrement operator (Prefix)
  2. Post-decrement operator (Postfix)

Pre-decrement Operator:

Pre-decrement operator is defind as when the decrement operator(--) is written before a variable then it is known as Pre-decrement operator or Prefix. The Pre-decrement operator works in such a way that, first the value is decremented by one(1), and then it returns the new decremented value.

Syntax:

Example:

Explanation:

  • In the very first statement, a value 12 has been assigned to a.
  • Then in the next statement, it performs two actions.
  • It decrements the value of a to 11.
  • Then assign the new decremented value to b.

Output:

Post-decrement Operator:

Post-decrement operators, also known as Postfixes, are created when the decrement operator (--) is written after a variable. The Post-decrement operator works in such a way that, the operator first returns the value of the operand and then at the end, the value is decremented by one(1).

Syntax:

Example:

Explanation:

  • In the above code we can see that, first the post-decrement operator assign the value in the left hand side variable b.
  • Then the operator decrements the value of a by 1.

Output:

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. It is represented by an exclamatory symbol (!).

Syntax:

Example:

Explanation:

  • In the code above, first, we declare two Boolean variables that are Var1 and Var2.
  • Then Logical Complement Operator (!) applies on Var1 and updated the value of Var1.
  • Then Logical Complement Operator (!) applies on the Var2 and updated the value of Var2.
  • The Boolean values of the variables are now reversed.

Output:

Conclusion

  • You can modify the sign, conduct an increment or decrement, or change the Boolean value (true/false) using the unary operators.
  • Unary Plus is used to represent positive values.
  • Unary Minus is used to convert positive value to negative value.
  • Increment operator is used to increment the value of a variable by one(1).
  • Decrement operator is used to decrement the value of a variable by one(1).
  • Logical Complement Operator is used to reverse the boolean value from true to false and vice-versa.