java operator precedence y = x+ (x=10); [duplicate] - java

This question already has answers here:
What are the rules for evaluation order in Java?
(5 answers)
Closed 6 years ago.
Let's consider
int x=1;
int y1 = (x=10) + x;
System.out.println(y1);
// prints 20
x = 1;
int y2 = x + (x=10);
System.out.println(y2);
// prints 11
In the first example, () is executed first as it has the highest precedence; sets x value to 10 and hence the right-hand operand (x) gets the new value 10 and so y1 = 10+10 = 20
In the second example, the left-hand operand 'x' gets its value 1, then () is executed and x gets its new value 10, so y2 = 1+10 = 11;
Why in the 2nd example, () doesn't get executed first; so (x=10) gets executed first and the left-hand operand x should be set to its new value 10 and hence y2 = 20; but it didn't happen this way?

I believe the operands are simply evaluated left-to-right. The parentheses in the right-hand operand don't change the fact that the left-hand operand gets evaluated first.

Maybe it helps to think about int z = f() + g(), i.e. invoking two methods. Why would g() be invoked first? What about int z = f() + (g()) (mind the additional parenthesis)? Left to right is what is happening, where (x=10) is just a construct that - when evaluated - assigns to x and returns 10 to where this construct is used.

Related

Tricky basic java operations [duplicate]

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.

Priority operators in Java [duplicate]

This question already has answers here:
What are the rules for evaluation order in Java?
(5 answers)
Closed 4 years ago.
For the next code, what it's z? (Java)
int x = 5;
int y = 10;
int z =++x*y--;
The order of priority is: y--, ++x, *, =.
( https://introcs.cs.princeton.edu/java/11precedence/ )
Why after run the code, z = 60 ?
The ++ operator is evaluated before the expression.
i.e.:
int x = 10;
int y = ++x; //y = 11
int z = x ++; // z = 11;
y-- is higher up on the list from your source. However, when the post-decrement happens, it happens after the whole evaluation.
So if you print y after getting the value of z, it will be 9.
And the pre-increment happens first, so ++x becomes 6 within that statement (and obviously multiplies by 10).
See an example in the docs.

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.

Variable assignment: int x = x = 1 [duplicate]

This question already has answers here:
Java - Order of Operations - Using Two Assignment Operators in a Single Line
(4 answers)
Closed 7 years ago.
This is allowed by the Java compiler, what is it doing?
int x = x = 1;
I realize that x is assigned to x, but how can it have two =s?
x = 1 both assigns the value 1 to x and also 'returns' 1, it allows for things like this:
while ((line = reader.readLine()) != null)
Read assignment statement from right to left:
Acording to Assignment Operators
There are 12 assignment operators; all are syntactically
right-associative (they group right-to-left). Thus, a=b=c means
a=(b=c), which assigns the value of c to b and then assigns the value
of b to a.
So,
int x = x = 1;
is the same as
x = (x = 1);
then
x = 1; x = x;
int x puts x on the stack.
The right hand part x = 1 assigns 1 to x. But this is an expression with value 1.
Finally this is re-assigned to x.

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