Unary Operators in Java

Unary Operators can be simply defined as an operator that takes only one operand and does a plain simple job of either incrementing or decrementing the value by one. Added, Unary operators also perform Negating operations for expression, and the value of the boolean can be inverted.

  • Unary Plus, denoted by '+'
  • Unary Minus, denoted by '-'
  • Unary Increment Operator, denoted by '++'

Post-Increment

Value is first processed then incremented. In post increment, whatever the value is, it is first used for computing purpose, and after that, the value is incremented by one.

Pre-Increment

On the contrary, Pre-increment does the increment first, then the computing operations are executed on the incremented value.

Post-Decrement

While using the decrement operator in post form, the value is first used then updated.

Pre-Decrement

With prefix form, the value is first decremented and then used for any computing operations.

Code Snippet

//Unary Operators in Java

public class App {
    public static void main(String[] args) {
        // Unary Operators in Java ++ --
        int a = 10;
        System.out.println(a);
        // a++; //a=a+1
        System.out.println(a++);
        System.out.println(a);
        System.out.println(++a);
    }
}