a += a++ * a++ * a++ in Java. How does it get evaluated? - java

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

Related

java - Which has greater precedence, + or -

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.

If post-increment/decrement has precedence over pre-increment/decrement, then why is this expression evaluated like so: [duplicate]

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.

When exactly does postfix unary operator happen?

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++”?

The difference between += and =+

I've misplaced += with =+ one too many times, and I think I keep forgetting because I don't know the difference between these two, only that one gives me the value I expect it to, and the other does not.
Why is this?
a += b is short-hand for a = a + b (though note that the expression a will only be evaluated once.)
a =+ b is a = (+b), i.e. assigning the unary + of b to a.
Examples:
int a = 15;
int b = -5;
a += b; // a is now 10
a =+ b; // a is now -5
+= is a compound assignment operator - it adds the RHS operand to the existing value of the LHS operand.
=+ is just the assignment operator followed by the unary + operator. It sets the value of the LHS operand to the value of the RHS operand:
int x = 10;
x += 10; // x = x + 10; i.e. x = 20
x =+ 5; // Equivalent to x = +5, so x = 5.
+= → Add the right side to the left
=+ → Don't use this. Set the left to the right side.
a += b equals a = a + b. a =+ b equals a = (+b).
x += y
is the same as
x = x + y
and
x =+ y
is wrong but could be interpreted as
x = 0 + y
It's simple.
x += 1 is the same as x = x + 1 while
x =+ 1 will make x have the value of (positive) one
Some historical perspective: Java inherited the += and similar operators from C. In very early versions of C (mid 1970s), the compound assignment operators had the "=" on the left, so
x =- 3;
was equivalent to
x = x - 3;
(except that x is only evaluated once).
This caused confusion, because
x=-1;
would decrement x rather than assigning the value -1 to it, so the syntax was changed (avoiding the horror of having to surround operators with blanks: x = -1;).
(I used -= and =- in the examples because early C didn't have the unary + operator.)
Fortunately, Java was invented long after C changed to the current syntax, so it never had this particular problem.
Because =+ is not a Java operator.

Diffrence between x++ and ++x? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there a difference between x++ and ++x in java?
I am reading the official Java tutorial and I don't get the difference between postfix and prefix (++x vs x++). Could someone explain?
++x: increment x; the value of the overall expression is the value after the increment
x++: increment x; the value of the overall expression is the value before the increment
Consider these two sections:
int x = 0;
System.out.println(x++); // Prints 0
// x is now 1
int y = 0;
System.out.println(++y); // Prints 1
// y is now 1
I personally try to avoid using them as expressions within a larger statement - I prefer standalone code, like this:
int x = 0;
System.out.println(x); // Prints 0
x++;
// x is now 1
int y = 0;
y++;
System.out.println(y); // Prints 1
// y is now 1
Here I believe everyone would be able to work out what's printed and the final values of x and y without scratching their heads too much.
There are definitely times when it's useful to have pre/post-increment available within an expression, but think of readability first.
++x increments x and then returns the value
x++ returns the value of x and then increments the variable
For example:
int x = 0;
int A = ++x; // A = 1
int B = x++; // B = 1
int C = x; // C = 2
Well, you get enough answers, I'm going just to worry you... Both post- and pre-increment operators can confuse code, so sometimes it is better to use just x+1 then you and other people definitely know what is going on there. Some examples:
int x = 5;
x = ++x;
System.out.println( x ); // prints 6
x = x++;
System.out.println( x ); // prints 6!
x = ++x + x++;
System.out.println( x ); // prints 14!
two last incrementing can be a source of problems to debug then (was watching that few times in my life...). x = x++ - it is evaluated before incrementing... So be careful!
++x is pre-incrementing and x++ is post-incrementing. With post-incrementing the value is increased after evaluation and with pre-incrementing the value is increased before evaluation.
Well, standing alone it's the same. but, if there are other operands involved - ++x will advance x and then apply the other operands, and x++ will first use x and then advance it.
Basically, ++x adds 1 to x before x is evaluated, while x++ adds 1 afterwards. It makes sense if you use it as an argument.
Let's start with x
int x = 3;
If we call System.out.println on x using the infix operator:
System.out.println(++x);
x will first increase to 4 and the println will output, "4". It is pretty much the same if we did:
x+=1;
System.out.println(x);
Let's imagine x equals 3 again. If we call System.out.println on x using the postfix operator:
System.out.println(x++);
It will first output the current value of x, "3", and then increase x. So it's like:
System.out.println(x);
x+=1;
Hope that helps.

Categories

Resources