Unary Operators

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 (++): The increment operator increases the value of the operand by 1. 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 (–): The decrement operator decreases the value of the operand by 1. 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.

Example:

public class UnaryOperatorExample {
    public static void main(String[] args) {
        int num = 35;

        // Unary Plus
        int resultPlus = +num;
        System.out.println("Unary Plus: " + resultPlus); 

        // Unary Minus
        int resultMinus = -num;
        System.out.println("Unary Minus: " + resultMinus); 

        // Increment (pre-increment and post-increment)
        int preIncrementResult = ++num;
        int postIncrementResult = num++;
        System.out.println("Pre-increment: " + preIncrementResult); 
        System.out.println("Post-increment: " + postIncrementResult); 

        // Decrement (pre-decrement and post-decrement)
        int preDecrementResult = --num;
        int postDecrementResult = num--;
        System.out.println("Pre-decrement: " + preDecrementResult); 
        System.out.println("Post-decrement: " + postDecrementResult); 

        
    }
}

Output:

Unary Plus: 35

Unary Minus: -35

Pre-increment: 36

Post-increment: 36

Pre-decrement: 36

Post-decrement: 36

Explanation:

Let’s go through the example and understand it.

We have a variable num set to 35.

Unary Plus: int resultPlus = +num; The unary plus operator (+) doesn’t change the value of the operand. So, resultPlus will have the same value as num.

Output: Unary Plus: 35

Unary Minus: int resultMinus = -num; The unary minus operator (-) negates the value of the operand. Since num is 35, resultMinus will have the value -35.

Output: Unary Minus: -35

Increment (pre-increment): int preIncrementResult = ++num; The pre-increment operator (++num) increments the value of num by 1 before using it in the expression. So, num becomes 36, and preIncrementResult is assigned the value 36.

Output: Pre-increment: 36

Increment (post-increment): int postIncrementResult = num++; The post-increment operator (num++) uses the current value of num in the expression and then increments num by 1. So, postIncrementResult is assigned the value 36, and num becomes 37.

Output: Post-increment: 36

Decrement (pre-decrement): int preDecrementResult = –num;  The pre-decrement operator (–num) decrements the value of num by 1 before using it in the expression. So, num becomes 36 again, and preDecrementResult is assigned the value 36.

Output: Pre-decrement: 36

Decrement (post-decrement): int postDecrementResult = num–;  The post-decrement operator (num–) uses the current value of num in the expression and then decrements num by 1. So, postDecrementResult is assigned the value 36, and num becomes 35 again.

Output: Post-decrement: 36

In summary, the code demonstrates the use of various unary operators, such as unary plus, unary minus, pre-increment, post-increment, pre-decrement, and post-decrement, on the variable num, and prints the results of these operations to the console.

 

 

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close