Open In App

Difference between x++ and x=x+1 in Java

Last Updated : 08 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In x++, it increase the value of x by 1 and in x=x+1 it also increase the value of x by 1. But the question is that both are same or there is any difference between them. We should aware with the fact that whenever we are trying to apply any arithmetic operator between two variables a and b, the result type is always max ( int, type of a, type of b). Let’s see now see difference between both of them :

  1. Internal Typecasting of data: In the below example, we are doing arithmetic operation i.e. addition on b and 1. Here b is of byte type and 1 is of int type. Therefore, the result should be of int type i.e max(int,type of b i.e. byte,type of 1 i.e. int). We are assigning int type to byte type in the above program that’s why we are getting compile time error saying “possible loss precision”. Here typecasting is required to perform addition.

Using x = x + 1

JAVA




// Java program to illustrate
// how arithmetic operations performed
// depends on data types
public class Test
{
    public static void main(String[] args)
    {
        byte b = 10;
         
        // Using b = b+1
        b = b + 1;
        System.out.println(b);
         
        /* Using typecasting will work
        b=(byte)b+1;
        System.out.println(b);*/
    }
}


  1. Output:
error: incompatible types: possible lossy conversion from int to byte
  1. Using Typecasting, output will be
11

Using x++

  1. In the next example, we are doing increment but internally we are doing operation like b++. The result should be of int type i.e max(int,type of b i.e. byte,type of 1 i.e. int) and we are getting the result as 11 because implicit typecasting is done by compiler like byte b=(byte)(b+1) here. 

JAVA




// Java program to understand the
// operations of ++ operator
public class Test
{
    public static void main(String[] args)
    {
    byte b = 10;
    b++;
    System.out.println(b);
    }
}


  1. Output:
11
  1. From the above example we can understand that in the increment/Decrement operator compiler automatically do type-casting whenever required. But how this happens? Let try to understand: Suppose We have to perform increment then we use ++ operator:
i++;
  1. is just a shortcut for:
i = i + 1;
  1. But what if we take values for i and j like this:
byte i = 1;
Then i = i + 1;
  1. will not compile because we are assigning int value to byte type and there is no typecasting in that statement but i++; will compile fine. It means that in fact i++; is a shortcut for something like this
i = (type of i)(i + 1);
  1. Different compiler instructions for both : They are different operators, and use different JVM instructions in bytecode.
x + 1 uses iadd instruction, 
whereas x++ uses iinc instruction internally
  1. Although this is compiler dependent. A compiler is free to use a different set of instructions for a particular operation.

For more details refer to this article: Interesting facts about Increment and Decrement operators in Java



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads