Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Medium
  • Last Updated : 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 :

  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 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);*/
        }
    }

    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 ++ operator
    public 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);
  2. 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 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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :