Given this code:
int p,k=8;
p=k*(++k-8);
System.out.println(p);
when ++k
is evaluated k=9
and then that becomes k*(9-8)
giving 9*1
int p,k=8;
p=(++k-8)*k;
System.out.println(p);
But this gives 9 as output
Given this code:
int p,k=8;
p=k*(++k-8);
System.out.println(p);
when ++k
is evaluated k=9
and then that becomes k*(9-8)
giving 9*1
int p,k=8;
p=(++k-8)*k;
System.out.println(p);
But this gives 9 as output
You have a multiplication with
left side: k
right side: (++k-8)
As you correctly stated, braces have precedence. However, your program still runs "from left to right". So first the left side is evaluated, which is k = 8
. Afterwards the right side is evaluated, which is (++k-8) = 1
.
Now we have determined both sides and can multiply them together: 8*1 = 8
.
this is the class file your code compiled:
int k = 8;
byte var10000 = k;
int k = k + 1;
int p = var10000 * (k - 8);
System.out.println(p);