Unary operators in java with example

Let’s learn unary operators in java with example.

Unary operators in java with example

Unary operator needs only one operand to increment, decrement, etc. Here are various unary operators,

  1. logical complement operator (!): reverses logical state or value of operand. If the value is false, it converts value to true and vice versa.
  2. unary minus (-): converts positive value to negative value.
  3. increment (++): increments value of an integer. post and pre-increment.
  4. decrement (–): decrements value of an integer. post and pre-decrement.
  5. unary plus (+): operator represents positive value. That is it makes all bits inverted, every 0 to 1 and every 1 to 0.

Here’s an example on ‘NOT’ (!) operator.

public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      boolean bool = true;
      int a = 14, b = 5;
      System.out.println("Before using NOT operator(!): " + bool);
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      System.out.println("After using using NOT operator(!): " + !bool);
      System.out.println("!(a < b) = " + !(a < b));
      System.out.println("!(a > b) = " + !(a > b));
   }
}


Output:

Before using NOT operator(!): true
a = 14
b = 5
After using using NOT operator(!): false
!(a < b) = true !(a > b) = false


Here’s an example on unary minus operator(-).

public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      int a = 5;
      System.out.println("Given number : " + a);
      a = -a;
      System.out.println("After using unary minus operator(-) : " + a);
   }
}


Output:

Given number : 5
After using unary minus operator(-) : -5


Increment (++) operator increments value of an integer and this operator can be used in two ways post-increment and pre-increment.

Post-increment operator: If increment operator is placed after variable name value of operand is incremented but previous value is retained temporarily till program execution.

Example: a++

a = 13

a++ = 14


Pre-increment operator:

If increment operator is placed before variable name, value of operand is incremented instantly.

Example: ++a

a = 13

++a = 14

Let’s see an example on unary increment (++) operator.

public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      int a = 7;
      System.out.println("post-increment = " + a++);
      System.out.println("pre-increment = " + ++a);
   }
}


Output:

post-increment = 7
pre-increment = 9


Decrement operator(–): decrements value of an integer and this operator in two ways post-decrement and pre-decrement.

Post-decrement operator: If increment operator is placed after variable name value of operand is decremented but previous value is retained temporarily till program execution.

Example: a–

a = 6

a– = 5

Pre-decrement operator: If decrement operator is placed before variable name, value of operand is decremented instantly.

Example: –a

a = 6

–a = 5

Let’s see an example on unary decrement (–) operator.

public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      int a = 7;
      System.out.println("post-decrement = " + a--);
      System.out.println("a = " + a);
      System.out.println("pre-decrement = " + --a);
   }
}


Output:

post-decrement = 7
a = 6
pre-decrement = 5


Also read – inheritance in java