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 types
// Using b = b+1
b = b +
/* Using typecasting will work
error: incompatible types: possible lossy conversion from int to byte
Using Typecasting, output will be
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 ++ operator
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:
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 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 email@example.com. 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.
- Difference between super() and this() in java
- Difference between Java and C language
- Difference between throw and throws in Java
- Difference between notify() and notifyAll() in Java
- Difference Between LinkedList and LinkedHashSet in Java
- Difference between print() and println() in Java
- Difference between an Iterator and ListIterator in Java
- Difference between ArrayList and HashSet in Java
- Difference between == and .equals() method in Java
- Difference between concat() and + operator in Java
- Difference between Arrays and Collection in Java
- Difference between an Integer and int in Java with Examples
- Difference between String and Character array in Java
- Difference between Scanner and BufferReader Class in Java