3
int b = 2;

c= b++;
System.out.Println("C :"+c+"B"+b);

c will be 2 and b will be 3.

int b = 2;

b= b++;
System.out.Println("B:"+b);

Here I wonder what is getting incremented. and where does that incremented value goes.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Since it's `b++`, the `b` is getting incremented after the statement is "processed", and the value goes to wherever specified; in these examples it doesn't go anywhere else. – Steve Smith Jul 04 '17 at 15:14
  • The important thing here is that `b++` is the same thing as `b = b + 1`. – luizfzs Jul 04 '17 at 15:15
  • Since my previous answer was wrong. I will just link the better explanation: https://stackoverflow.com/questions/27621242/difference-between-b-b-and-b – Rence Jul 04 '17 at 15:20

3 Answers3

4

Well this is tricky. Let's take a look what the post-increment operator does. The JLS states:

The value of the postfix increment expression is the value of the variable before the new value is stored.

So in your code b++ has the value 2, independently from the incrementation of b.

If you do an assignment, the right-hand side expression is evaluated first and then its value is stored into the variable.

It's straightforward if you do c = b++. The expression b++ has the value 2 which gets assigned to c. After that statement b has the incremented value 3. But that's a little simplified. What actually happens is, that b++ is evaluated first, meaning b is incremented before the assignment takes place, not after.

So what happens when you do b = b++? Well, it's just the same: The b++ is evaluated first, meaning b is incremented. After that the assignment takes place and the value of the expression b++ is assigned to b. This is still 2 and overwrites your incremented b.

Fun fact: If I enter your second code in my Eclipse, Findbugs issues the following warning:

Bug: Overwritten increment in MyClass.main(String[])

The code performs an increment operation (e.g., i++) and then immediately overwrites it. For example, i = i++ immediately overwrites the incremented value with the original value.

Community
  • 1
  • 1
André Stannek
  • 7,773
  • 31
  • 52
  • Well, it is not even fun that Eclipse needs a plugin to find out that the increment isn't used and therefore redundant. But that's just Eclipse :/. – Tom Jul 04 '17 at 15:54
  • @Tom Do you know an IDE that would have warned about this out of the box? (Disclaimer: Honest question, not trying to start an IDE war :D) – André Stannek Jul 04 '17 at 15:56
  • 1
    IntelliJ Idea does that. It has a full set of inspections and unused assignments (like `String s = "A"; s = "B";` or `i = i++;`) is one of them. – Tom Jul 04 '17 at 15:59
  • 1
    @Tom that's nice. Eclipse does show a few such things but not this particular case. I always go with Findbugs anyway since it finds a lot of not so obvious things. – André Stannek Jul 04 '17 at 16:05
  • And it is a good thing that Eclipse supports such Plugins and they definitely should be used, but I would prefer some more core features already implemented into Eclipse. But that's just a matter of taste. Plugins still can provide the same features. – Tom Jul 04 '17 at 16:08
1

I guess you just don't get what happens here:

int b = 2;

b= b++;
System.out.println("B:"+b);

And more specifically, this line:

b = b++;

The compiler sees that this is an assignment statement, so it tries to evaluate the expression on the right first:

b++

Since ++ here is a post increment operator, the expression just evaluates to b, i.e. 2. Immediately after the evaluation, b is incremented to 3.

We're not done yet! This is an assignment statement remember? Now that the right side is evaluated, the expression now looks like this:

b = 2;

Great! Now b is assigned the value of 2.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1
int b=2    
b = b++

Is like you are running this code

int b = 2;
int temp = b;
b = b + 1;
b = temp;
Ebrahim Poursadeqi
  • 1,776
  • 2
  • 17
  • 27