Eleeist
Eleeist

Reputation: 7041

Difference in implementation of x = x + 1 and x++

My professor recently said that although x = x + 1 and x++ will obviously give the same result, there is a difference in how they are implemented in the JVM. What does it mean? Isn't compiler like: hey, I see x++ so I will switch it to x = x + 1 and carry on?

I doubt there is any difference when it comes to efficiency, but I would be surprised if assembly would be different in those cases...

Upvotes: 10

Views: 13897

Answers (3)

user85421
user85421

Reputation: 29680

The statement "obviously give the same result" is wrong:

Differences

not the same result:

int x = 10;
int a = x++; // a = 10
int x = 10;
int b = x = x + 1; // b = 11

compile error

x = x + 1 will not compile if the type of x is neither int nor long - x+1 is always compiled as an int addition, that is, x is promoted to int; unless x is long:

byte x = 10;
x = x + 1; // incompatible types: possible lossy conversion from int to byte

Bytecode

If the generated bytecode is the same or not is probably implementation dependent; probably even on how these expressions are being used - is the result needed, or just the value of x.
You can use javap to check the bytecode for your compiler.

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

My professor recently said that although x = x + 1 and x++ will obviously give the same result

I guess your professor perhaps meant - the value of x after x = x + 1 and x++ will be same. Just to re-phrase, as it seems to be creating confusion in interpreting the question.

Well, although the value of x will be same, they are different operators, and use different JVM instructions in bytecode. x + 1 uses iadd instruction, whereas x++ uses iinc instruction. Although this is compiler dependent. A compiler is free to use a different set of instructions for a particular operation. I've checked this against javac compiler.

For eclipse compiler, from one of the comment below from @Holger:

I just tested it with my eclipse and it produced iinc for both expressions. So I found one compiler producing the same instructions

You can check the byte code using javap command. Let's consider the following class:

class Demo {
    public static void main(String[] args) {
        int x = 5;

        x = x + 1;
        System.out.println(x);

        x++;
        System.out.println(x);
    }
} 

Compile the above source file, and run the following command:

javap -c Demo

The code will be compiled to the following bytecode (just showing the main method):

 public static void main(java.lang.String[]);
   Code:
      0: iconst_5
      1: istore_1
      2: iload_1
      3: iconst_1
      4: iadd
      5: istore_1
      6: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      9: iload_1
     10: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
     13: iinc          1, 1
     16: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
     19: iload_1
     20: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
     23: return

Upvotes: 29

Ingo
Ingo

Reputation: 36329

The two expressions x++ and x=x+1 will not give the same result, your professor is wrong (or you confused this with ++x, which is again different). To see this

void notthesame() {
    int i = 0;
    System.out.println(i = i + 1);
    i = 0;
    System.out.println(i++);
    System.out.println("See?");
}

Hence, the question for bytecode is meaningless, because 2 different computations can't have the same bytecode.

Upvotes: 7

Related Questions