-1

I got these 2 different codes in Java that I can't understand how they produce these specific outputs.

They seemed to work in 2 different logic and I can't figure it out!

int a = 5;
int b = 4;
a -= (b++) + ++a; // For more confusing results replace -= with += and see how the logic changes.
System.out.println(a); // Output: -5

and

int c = 8;
c += ++c;
System.out.println(++c); // Output: 18

How does each situation work and how are they producing these specific outputs?

Themelis
  • 4,048
  • 2
  • 21
  • 45
  • 2
    You should check the difference between post and preincrement – Norhther Sep 23 '19 at 19:53
  • 1
    Not a dupe, as depends on how `a -= x` and `++a` interact. The key point seems to be the rewriting `a -= x` as `a = a -x` happens before a pre increment operator. – Salix alba Sep 23 '19 at 22:43
  • I was unsure to ask this simple question but really struggled with it, and I didn't know what to search on google because I already know how increment, decrement, pre and post work so I decided to ask here which was my last solution. Before I asked here and while I was still playing around with these operations that I posted, in my mind and for some reason, I was sure that I tried b++ and (b++) and realized that (b++) acts like ++b, however, I was certainly wrong. But thanks for the quick response from everyone especially Nexevis. – Alex Schmidt Sep 24 '19 at 21:55

5 Answers5

2

The major difference here is what post and pre increment are. These determine whether the value is increased before evaluation, or afterward.

Here is how the first case breaks down mathematically:

a = 5 - (4 + 6), which reduces to -5.

Note that a is increased from ++a AKA preincrement before the math is done, however b is calculated as 4. Another thing to note is the a used from the -= uses the original a value, regardless of post or pre increment.

The second equation reduces to this mathematically:

c = 8 + 9 which reduces to 17.

The output prints 18 because your System.out.print(++c) increments it one more time before output due it being preincrement. Note if the print statement used c++, the value would print to 17.

The chart for operator precedence can be found here. Note that assignment has lower precedence than the unary/postfix operators.

Nexevis
  • 4,647
  • 3
  • 13
  • 22
1

It's all about the Operator Precedence in Java. Check that table and figure out which operation takes place first and which last.

Themelis
  • 4,048
  • 2
  • 21
  • 45
0

It is equivalent to:

int a = 5;
int b = 4;
a -= (b++) + ++a; // => 5 -= (4) + 6
int c = 8;
c += ++c; // => 9 += 9

The main diff is thet:

++a and ++c increments the value and immediately returns it.

b++ returns the value and then increments it.

Eugen
  • 877
  • 6
  • 16
0

There is a difference in the order of ++. While both increase the variable, ++a will pass the original value to the operation chain your in middle of; while a++ would pass the new value. So for your first example:

++a --> a is now 6; but the equation is using 5:
a -= (b++) + 5;
b++ --> b is now 5;
a -= 5 + 5;
a -= 10;
? = 5 - 10;


a = a - 5 + 5;
a = 5 - 5 + 5;
a = -10;

(You should have enough to trace the second example).

For a list of operations try this. Some more increment examples are here.

CeePlusPlus
  • 803
  • 1
  • 7
  • 26
0

A minimal example is `

int x=3,y=3;
x += ++x;
y+= y++;

at the x x is 7 and y is 6. Precedence alone is not enough to explain the behaviour. Using precedence the second line would be x += (++x), i.e. increment x and return its value, (x is now 4); next we have x+=4 which would return 8.

Instead, it seems better to treat x += w as a short hand for x = x + w, this rewriting happens before evaluation. In our case the rewriting is x = x + ++x interpreted as x = (x + (++x)). So interpreted as

x = ( 3 + (++x))
x = ( 3 +  4 )       x is 4
x = 7                x is 7

A simlar system works to the y equation giving y = 6 at the end.

Salix alba
  • 7,536
  • 2
  • 32
  • 38