Considering this expression:
x1 = ++x - x++ + --x
if the value of x is entered 5
What will be the output of such expression in java, and why?
There is no greater precedence in + or -.
Without parentheses they are just executed in the order of apperance, lets consider your example:
public class Test {
public static void main(String[] args) {
int x = 5;
int x1 = ++x - x++ + --x;
System.out.println(x1);
}
}
will print 6
There are four operations of pre/post incrementation/decrementation:
++x will increment before evaluation (before using its value in the expression)
--x will decrement before evaluation
x++ will increment after evaluation (after using its value in the expression)
x-- will decreemnt after evaluation
Breaking the expression to parts:
1) "++x" the x will be incremented by 1 before using its value in the expression, so for now it is:
6 -
2) "- x++" the x will be incremented by one after using its value in expression, so it is:
6 - 6 , but now x=7
3) "+ --x" the x will be decremented by one, before its value will be used, so it finally translates to:
x1 = 6 - 6 + 6;
This will be executed as:
x1 = 0 + 6;
x1 = 6;
Obviously, there is no greater precedence in + or -, as it is just subtracted and added in the order of apperance.
Related
This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 3 years ago.
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?
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.
It's all about the Operator Precedence in Java. Check that table and figure out which operation takes place first and which last.
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.
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.
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.
This question already has answers here:
What are the rules for evaluation order in Java?
(5 answers)
Closed 6 years ago.
public class Test {
public static void main(String[] args) {
int x = 3;
int y = ++x * 5 * x--;
System.out.println("x is " + x);
System.out.println("y is " + y);
}
}
The output is:
x is 3
y is 80
But using the rule that post-operators take precedence over pre-operators, shouldn't it be like this:
y = ++x * 5 * 3 (x is 2)
y = 3 * 5 * 3 (x is 3)
y = 45
Instead, this code is acting as if it just evaluated the expression from left-to-right, evaluating the pre-increment before the post-decrement. Why?
int y = ++x * 5 * x--;
++x => Increase then evaluate => x == 4
x-- => Evaluate then decrease
So actually it looks like this:
int y = 4 * 5 * 4; // == 80
and because of the decrement operator, your x is 3 at the end.
Your problem is y = ++x * 5 * 3 (x is 2) its not (4)*5*3 its (4)*5*4. Pemdas my friend
int x = 3;
int y = ++x * 5 * x--; //x becomes 4 , then its 4*5*4 =80 = y
System.out.println("x is " + x); // x-- comes into affect and x = 3
System.out.println("y is " + y); // y = 80
Although x-- is being evaluated before x++ and everything else. It changes the value of x after the whole expression is evaluated. Unlike ++x when it get evaluated it changes the value of x immediately.
I read a few post regarding unary operator:
What is the Difference between postfix and unary and additive in java
"C - C++" joke about postfix/prefix operation ordering
And a few more.
However, I still don't understand exactly when the value is changed.
For example:
int x = 1;
x = x++;
System.out.print("x = x++ ==> ");
System.out.print(" x = " + x);
System.out.println();
int x = 1;
x = x++ + x++;
System.out.print("x = x++ + x++ ==> ");
System.out.print(" x = " + x);
System.out.println();
The output is:
x = x++ ==> x = 1
x = x++ + x++ ==> x = 3
So in the first block x is assigned to x and afterwards incremented, but the value is never used, otherwise the output would have been x = 2.
In the second block, if I understand correctly, the first x++ is evaluated before the assignment and the second x++ is evaluated afterwards but is never used.
If in the second block both x++ would have been evaluated after the assignment but never used, the output would have been x = 2. If both have been used, the output would have been x = 4.
My IDE also indicated that the first x++ is used, but the second is not used:
So to conclude - I'm still confused about when and how exactly the increment is done.
At the line
x = x++ + x++;
Assuming x = 1, the first x++ returns "1" as the value, and then it increments x to 2. So basically, it's assigning the old value back to x.
The second x++ does the same; it returns the value of x, which is now 2, and only then increments its value to 3 - that value, is not used.
Your code is equivalent to:
tmp = x;
x = x + 1;
tmp2 = x;
x = x + 1; // not used
x = tmp + tmp2;
Links that may help you:
JSL - 15.14.2. Postfix Increment Operator ++
JLS - 15.15.1. Prefix Increment Operator ++
What is x after “x = x++”?
I was running the following code
int x=4;
int y=3;
double z=1.5;
z=++x/y*(x-- +2);
int t=(++x/y);
System.out.println(z); //7
wondering how does it produce 7 when
(x-- +2) =6
++x/y=1.6666
3=6*1.6666=10
z=++x/y*(x-- +2);
is evaluated as:
z = ++x / y * (x-- + 2); // Substitute value of ++x, y and x--
= 5 / 3 * (5 + 2); // After this point, x will be 4. Evaluate parenthesized expr
= 5 / 3 * 7 // Now, left-to-right evaluation follows
= 1 * 7 // 5 / 3 due to integer division will give you 1, and not 1.66
and:
t = ++x / y; // x is 4 here
= 5 / 3
= 1
The code is evaluated as:
z=((++x)/y)*(x-- +2);
x and y are both int, so the calculation results of each step will be cast into int type. Which means 5/3=1.
In the end, the result is assigned to a double variable, so 7 will be cast to 7.0.
Modify the code to:
z=1.0 * ((++x)/y)*(x-- +2);
You'll get a decimal result.
I came across this problem in this website, and tried it in Eclipse but couldn't understand how exactly they are evaluated.
int x = 3, y = 7, z = 4;
x += x++ * x++ * x++; // gives x = 63
System.out.println(x);
y = y * y++;
System.out.println(y); // gives y = 49
z = z++ + z;
System.out.println(z); // gives z = 9
According to a comment in the website, x += x++ * x++ * x++ resolves to x = x+((x+2)*(x+1)*x) which turns out to be true. I think I am missing something about this operator precedence.
Java evaluates expressions left to right & according to their precedence.
int x = 3, y = 7, z = 4;
x (3) += x++ (3) * x++ (4) * x++ (5); // gives x = 63
System.out.println(x);
y = y (7) * y++ (7);
System.out.println(y); // gives y = 49
z = z++ (4) + z (5);
System.out.println(z); // gives z = 9
Postfix increment operator only increments the variable after the variable is used/returned. All seems correct.
This is pseudocode for the postfix increment operator:
int x = 5;
int temp = x;
x += 1;
return temp;
From JLS 15.14.2 (reference):
The value of the postfix increment expression is the value of the variable before the new value is stored.
Nothing to do with operator precedence per se, just the order of evaluation.
Two things to know here:
x++ is the postfix increment, so the value of x is incremented after it is evaluated
* evaluates the right side then the left side.
Considering point 2, the expression x++ * x++ * x++ can be rewritten more specifically as x++ * (x++ * (x++)).
The whole expression can be written as the procedures:
a = x
x += 1
b = x
x += 1
c = a*b
d = x
x += 1
return c*d
The postfix operator x++ means something like "give me the value of x now, but increment it for future references"
So, by the order of operations and evaluation,
x++ * x++ * x++
is interpreted first as
3 * 4 * 5 (=60)
Which is then added to the original 3, yielding 63.
The original value is used because it's on the same line, had you written something like:
int x = 3;
int y += x++ * x++ * x++;
x += y;
x would now be 66, instead of 63 because the x in the second line is now 6, rather than its original 3.
Because the increment Operation ++ is added after the variable x. That's a post increment operation. That means, x is incremented after the operation is handled.
In your example the expression would be:
x += 3 * 4 * 5
First the expression is added by 3 (x+=....)
then the first x++ results in 3
the second x++ results in 4 (because it was incremented before)
and the third x++ results in 5.
If you want your variable incremented before the operation is executed, you have to write ++x (pre increment operation)
Because a postincrement modifies the variable after the value is taken and += evaluates its left hand side before evaluating its right hand side,
x += x++ * x++ * x++;
becomes
tmp0 = x
tmp1 = x
++x
tmp2 = tmp1 * x
++x
tmp3 = tmp2 * x
++x
x = tmp0 + x
unary operators evaluated left to right, so the first x++ gets the value x, the second is (x+1), etc. And the += evaluates according to the value of x at the start, hence the addition of x