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.

  1. Unary Plus, denoted by "+"
  2. Unary minus, denoted by "-"
  3. 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.

Unary operators called increment (++) and decrement (--) operators. These operators increment and decrement value of a variable by 1.

Source Code

public class Unary {
    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);
 
 
    }
}
 

Output

10
10
11
12
To download raw file Click Here
`

Basic Programs