Unary Operator in Java | Pre, Post Increment

The operator that acts on a single operand is called unary operator in Java. A unary operator uses a single variable. There are three types of unary operators in java. They are as follows:

1. Unary minus operator ( – )
2. Increment operator ( ++ )
3. Decrement operator ( — )

Unary minus operator in Java


The operator which is used to negate a given value is called unary minus operator in java. Let’s understand it with an example program.
Program source code 1:

package unaryOperatorPrograms; 
public class MinusExample 
{ 
public static void main(String [] args) 
{ 
 int x = 5; 
  x =-5; 
 System.out.println(x); 
 }
}
Output: 
       -5

In this code snippet, the value of variable x is 5 in the beginning. When we applied the unary minus ( – ) operator on it then it became -5 because the unary minus operator has negated its value. Negation means the conversion of negative value into positive value and vice-versa.

Increment ( ++ ) Operator in Java


The operator which is used to increase the value of a variable (operand) by one is called increment operator in java. It is represented by ( ++ ). The operator ++ adds 1 to the operand. It can have the following forms: 
         ++x; or x++;
 ++x is equivalent to x = x + 1; (or x += 1; ).

Consider the following examples:
    1. int a = 5;
             ++a;
    2. int a = 5;
             a++

Here, the value of variable a is incremented by 1 when ++ operator is used before or after x. In Java, both are valid expressions.



Let’s take one more example to understand it better.
          a = a + 1;
In this expression, if we a is equal to 5, then a + 1 value will be 6. This value will be stored again left-hand side of variable x. Thus, the value of x now becomes 6.

The same thing can be performed by using ++ operator also. ++ operator can be used into two forms:

1. Pre incrementation (Prefix):

When we write ++ operator before a variable, it is called pre incrementation or prefix. In pre incrementation, the increment is done first and then any other operation will be performed.


In other words, the prefix operator first adds 1 to an operand, and then the result is stored in the left-hand side variable.

2. Post incrementation (Postfix):

When we write ++ operator after a variable, it is called post incrementation or postfix. In post-increment, all the other operations are performed first, and then at the end increment is done only.

In other words, the postfix operator first stores the value in the left-side variable and then the operator adds 1 to an operand.

 

Let’s see an example program related to this concept to understand better.
Program source code 2:

package operatorPrograms; 
public class IncrOperatorTest1 
{ 
public static void main(String[] args) 
{ 
 int i = 0; 
 System.out.println(i); // Output 0 
 System.out.println(++i); // Output 1, first increment the value of i and then displays it as 1. 
 System.out.println(i); // Output 1, displays the value of i because i is already incremented, i.e. 1 at right-hand side. 
 System.out.println(i++); // Output 1, first displays the value of i as 1 and then increment it. 
 System.out.println(i); // Output 2, displays the incremented value of i, i.e. 2. 
 } 
}
Output: 
       0 
       1 
       1 
       1 
       2

Explanation:
1. The first pre-increment operator increases the value of i by 1 and outputs the new value of 1.
2. The next post-increment operator also increases the value of i by 1 but outputs the value of i as 1 before increment occurs.
3. After the post-increment, the output is displayed as 2.



Program source code 3:
package operatorPrograms; 
public class IncrOperatorTest2 
{ 
public static void main(String[] args) 
{ 
 int x = 50; 
 int y = 100; 
 int z = 200; 
int a, b, c; 
a = ++x; 
b = y++; 
c = x + y++ + ++z; 
System.out.println("x = " +x); 
System.out.println("y = " +y); 
System.out.println("z = " +z); 
System.out.println("a = " +a); 
System.out.println("b = " +b); 
System.out.println("c = " +c); 
 } 
}
Output: 
       x = 51 
       y = 102 
       z = 201 
       a = 51 
       b = 100 
       c = 353

Explanation:
In the above example program, the initial values of x, y, z are 50, 100, and 200 respectively.

1. When the statement a=++x is executed, the value of x is incremented first by 1 and then the value of x is displayed as 51.

After incrementing the value of x is stored into the variable a. Therefore, the value of a is also displayed as 51 (same as that value of x). Thus, this operation happened in the below form:
         x = x + 1;
           a = x;

2. When the statement b=y++ is executed, the current value of y is stored into the variable b because the increment operator is in postfix form. Therefore, the value of b is displayed as 100.

After storing the current value of y into b, y’s value is incremented by 2 because y is modified two times in the program and then it is assigned to y. Therefore, the value of y is displayed as 102. 

Thus, this operation occurs as follows:
       b = y;
       y = ( y + 1) + 1;

3. When the statement c = x + y++ + z++ is executed, the value of x is assigned as 51 because the value of x is now 51 after increment. The value of y is incremented by 1 and then it is assigned as 101 into y because y++ is in postfix form.

Similarly, the value of z is also incremented by 1 and then it is assigned 201 into z. The sum of three values 51 + 101 + 201 will be stored into the variable c. Thus, the output is 353.

Let’s take one more example program to understand the concept better.
Program source code 4: 
Let’s find the value of following expression ++x*x++, given that the value of x is 10.

package operatorPrograms; 
public class IncrOperatorTest3 
{ 
public static void main(String[] args) 
{ 
 int x = 10; 
 int m = ++x * x++; 
System.out.println("m = " +m); 
 } 
}
Output: 
      m = 121

Explanation:

1. In the above program, the value of variable x is 10. When ++x is executed, the operator will increment the value of x by 1 and then it will assign the value of x as 11.

2. When x++ is executed, the value of x will not be incremented in the statement because this is post incrementation. Hence, the value of x will stay the same i.e. 11. Thus, the output will be 11 * 11 = 121.

Let’s understand more about increment operator with this complex example program.
Program source code 5: 
Find the value of x and y in the following expression, given that the value of x is equal to 20. 

package operatorPrograms; 
public class IncrOpertaorTest4 
{ 
public static void main(String[] arg) 
{ 
 int x = 20; 
 int y = ++x * 10 / x++ + ++x; 
System.out.println("x = " +x); 
System.out.println("y = " +y); 
 } 
}
Output: 
       x = 23 
       y = 33

Explanation: This question is more complicated than the previous example. So, let’s see the explanation of above program.

1. 1. When ++x will be executed in the numerator of expression, first, value’s x is incremented by 1 and then returned to the expression as 21, which is multiplied by 10. So, the operation will occur like this:
         int y = 21 * 10 / x++ + ++x; // x assigned value of 21.

2. Next, when x++ in the denominator of expression will be executed, the value of x is again incremented by 1 but the original value 21 of x will use in the expression because it is post-increment. So, the next operation will be like this:
         int y = 21 * 10 / 21 + ++x; // x assigned value of 21.

3. The final assignment of x increments the value of x by 1 because it is pre-increment. So, the value of x is now 23 because, after post-increment, the value of x returned as 22 to the ++x. We can simplify this:
         int y = 21 * 10 / 21 + 23; // x assigned value of 23.

4. Finally, we can easily evaluate multiply and division from left to right and perform simple addition. Thus, the final value of x will be printed as 23, and value of y is 33. 

Decrement ( — ) Operator in Java


The operator which is used to decrement the value of a variable (operand) by one is called decrement operator in java. It is represented by ( — ). The operator — subtracts 1 to the operand. It can have the following forms: 
         –x; or x–;

         –x is equivalent to x=x – 1; (or x -=1; ).

Like ++ operator, — operator can also be used into two forms:

1. Pre decrementation (Prefix):

When we write — operator before a variable, it is called pre decrementation or prefix. In pre decrementation, the decrement is done first and then any other operation will be performed.

In other words, the prefix operator first subtracts 1 to an operand, and then the result is stored in the left-hand-side variable.

2. Post decrementation (Postfix):

When we write — operator after a variable, it is called post decrementation or postfix. In post-decrement, all the other operations are performed first, and then at the end increment is done only.

In other words, the postfix operator first stores the value in the left-side variable and then the operator subtracts 1 to an operand. Post decrement is performed after all other operations are carried out.

Consider the following examples:
   1. int a = 5;
       int p = –a; // pre-decrement.
Here, the value of variable a is decremented by 1 and then assigned to a. In both cases, the value of a and p would be 4. The operation will occur like this;
       a = a – 1;
       a = 5 -1;
       a = 4;
       p = 4;

    2. int a = 5;
        int q = a–; // post-decrement.
Here, first, the original value of a will be assigned to variable q and then it is decremented by 1. In this case, the value of a is 4, and q is 5. The operation will be like this:
        q = 5;
        a = a – 1;
        a = 5 -1;
        a = 4;

Let’s take a simple example program related to these concepts.
Program source code 6:

package operatorPrograms; 
public class DecrOperatorTest1 
{ 
public static void main(String[] args) 
{ 
 int a = 1; 
System.out.println(a); // Output 1 
System.out.println(--a); // Output 0 
System.out.println(a); // Output 0 
System.out.println(a--); // Output 0 
System.out.println(a); // Output -1 
 } 
}
Output: 
       1 
       0 
       0 
       0 
      -1

Let’s take a complex example program based on all the above concepts.
Program source code 7:

package operatorPrograms; 
public class DecrOperatorTest2 
{ 
public static void main(String[] args) 
{ 
int a = 1; 
 ++a; 
int b = a++; 
 a--; 
int c= --a; 
int x = a * 10 / (b - c); 
System.out.println("a = " +a); 
System.out.println("x = " +x); 
 } 
}
Output: 
       a = 1 
       x = 10

Final words
Hope that this tutorial has covered all the important points related to the unary operator in java. I hope that you will have understood pre-increment and post-increment in Java nicely and enjoyed programming.
Thanks for reading!!!
Next ⇒ Conditional Operator in Java⇐ PrevNext ⇒

Leave a Comment