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

23 Nov, 2017
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 :
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 program to illustrate// how arithmetic operations performed// depends on data typespublic 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);*/    }}Output:error: incompatible types: possible lossy conversion from int to byte
Using Typecasting, output will be11Using x++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 program to understand the // operations of ++ operatorpublic class Test {    public static void main(String[] args)    {    byte b = 10;    b++;    System.out.println(b);    }}Output:11
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++;is just a shortcut for:i = i + 1;But what if we take values for i and j like this:byte i = 1;
Then i = i + 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 thisi = (type of i)(i + 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 internallyAlthough this is compiler dependent. A compiler is free to use a different set of instructions for a particular operation.
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 program to illustrate// how arithmetic operations performed// depends on data typespublic 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);*/    }}Output:error: incompatible types: possible lossy conversion from int to byte
Using Typecasting, output will be11Using x++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 program to understand the // operations of ++ operatorpublic class Test {    public static void main(String[] args)    {    byte b = 10;    b++;    System.out.println(b);    }}Output:11
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++;is just a shortcut for:i = i + 1;But what if we take values for i and j like this:byte i = 1;
Then i = i + 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 thisi = (type of i)(i + 1);
Using x = x + 1
// Java program to illustrate// how arithmetic operations performed// depends on data typespublic 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);*/    }}
Output:
error: incompatible types: possible lossy conversion from int to byte

Using Typecasting, output will be
11
Using x++
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 program to understand the // operations of ++ operatorpublic class Test {    public static void main(String[] args)    {    byte b = 10;    b++;    System.out.println(b);    }}
Output:
11

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++;
is just a shortcut for:
i = i + 1;
But what if we take values for i and j like this:
byte i = 1;
Then i = i + 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);
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 internallyAlthough this is compiler dependent. A compiler is free to use a different set of instructions for a particular operation.
x + 1 uses iadd instruction, 
whereas x++ uses iinc instruction internally
Although this is compiler dependent. A compiler is free to use a different set of instructions for a particular operation.
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
java-basics
java-puzzle
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Stack vs Heap Memory Allocation
Difference between Process and Thread
Difference Between Method Overloading and Method Overriding in Java
Difference between Clustered and Non-clustered index
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java