d9h
d9h

Reputation: 89

Incrementing variables

I understand the difference between post-increment and pre-increment, but I don't understand this:

int b = 1;
b = b++;
System.out.println(b); // --> 1

Why does this print 1? Isn't b incremented after b gets the value of b++ (which is 1)?

int x = 1;
b = x++;
System.out.println(b); // --> 1
System.out.println(x); // --> 2

This is the behaviour I would expect. What is different when using the same variable on both sides (b = b++)?

Upvotes: 0

Views: 175

Answers (4)

TheLostMind
TheLostMind

Reputation: 36304

If you look at the byte code closely.. You can observe the difference.. Changing your code a little..

public static void main(String[] args) {
    int b = 1;
    b = b++;
    System.out.println(b); // --> 1
    b=1;
    b=++b;
    System.out.println(b); // --> 2
}

Byte code :

public static void main(java.lang.String[]);
   descriptor: ([Ljava/lang/String;)V
   flags: ACC_PUBLIC, ACC_STATIC
   Code:
     stack=2, locals=2, args_size=1
        0: iconst_1          --> int constant 1
        1: istore_1           --> store 1 in slot 1 of var table  i.e, for b  
        2: iload_1           --> load value of b from stack --> load 1
        3: iinc          1, 1  --> increment value b      --> store 2 for b 
        6: istore_1            --> store value of b "which was loaded"      --> store 1 to b.. So effectively 1 
        7: getstatic     #22                 // Field java/lang/System.out:Ljava
/io/PrintStream;
       10: iload_1
       11: invokevirtual #28                 // Method java/io/PrintStream.print
ln:(I)V
       14: iconst_1
       15: istore_1            --> store 1 in slot 1 of var table  i.e, for b
       16: iinc          1, 1  --> increment  --> b=2
       19: iload_1             --> load  2 
       20: istore_1            --> store 2.. So, effectively 2
       21: getstatic     #22                 // Field java/lang/System.out:Ljava
/io/PrintStream;
       24: iload_1
       25: invokevirtual #28                 // Method java/io/PrintStream.print
ln:(I)V
       28: return
     LineNumberTable:
       line 13: 0
       line 14: 2
       line 15: 7
       line 16: 14
       line 17: 16
       line 18: 21
       line 25: 28
     LocalVariableTable:
       Start  Length  Slot  Name   Signature
           0      29     0  args   [Ljava/lang/String;
           2      27     1     b   I

Upvotes: 2

cнŝdk
cнŝdk

Reputation: 32145

In fact the b++ is a postincrement operation, and it's incrementing the value of your b variable but the output will not be affected.

In other words the variable b is incremented but the output is the old value of b.

I think this example will give you a better idea:

int b = 1;

System.out.println(++b); // --> 2
System.out.println(b++); // --> 1
System.out.println(b); // --> 2

Upvotes: 0

splrs
splrs

Reputation: 2432

You should read that right to left:

b++ increments b to 2 and returns a copy of the original value: 1

b = will recieve the value on its right side, which is the 1 returned by b++.

Upvotes: 1

Sebastian Redl
Sebastian Redl

Reputation: 71889

Because of the order of evaluation. In the statement b = b++;, first the right hand side is completely evaluated. This means that b's value changes from 1 to 2, and the result of the expression is 1 (the old value). Then the assignment is performed, overwriting b to be 1 again.

To be more explicit, the first example is, in behavior, equivalent to this code:

int b = 1;
int temp = b++; // b == 2, temp == 1
b = temp; // b == 1
System.out.println(b); // --> 1

Upvotes: 1

Related Questions