Post decrement, what is a-- * a--?

412 Views Asked by At
int a = 10;
System.out.println(a-- * a--);

what will be the output of these print statement and why??

in post decrement we actually observe that

  • the value is used before getting change

but here I am getting in one of the operator (a--) is 10 and in another operator (a--) with which we are multiplying is getting 9.

overall the output which I came across is 100 but the actual output is 90

please explain how?

1

There are 1 best solutions below

3
Zabuzard On

Java sees A * B, so it first needs to compute both arguments, A and B.

The left operand (A) a-- is executed, making a = 9, but returning the value it had before, so 10.

We now have 10 * a--. Next, Java executes the right operand (B) and we get a = 8, but we use the value it had before the decrement, so 9.

We get 10 * 9, which is 90. And a = 8.