问题
I had questions about incremental and decremental operators.I couldn't understand why java gave these outputs.
x = 5; y = 10;
System.out.println(z = y *= x++); // output is 50
x = 2; y = 3; z = 4;
System.out.println("Result = "+ z + y++ * x); // output is Result = 46
x = 5;
System.out.println( x++*x); // output is 30
x = 5;
System.out.println( x*x++); // output is 25
For example, in 2nd println function y is multiplicated without increasing 1 and in 3rd function x is multiplicated with x+1. As I know unary increment and unary decrement operators have higher precedence than arithmetic operators so why second one calculated without increasing 1( y++ * x = 3*2 = 6 there and why not (y+1) * x = 8 ?
回答1:
x = 5; y = 10;
System.out.println(z = y *= x++); // output is 50 -->z=y=y*x i.e, z=y=10*5 (now, after evaluation of the expression, x is incremented to 6)
x = 2; y = 3; z = 4;
System.out.println("Result = "+ z + y++ * x); // output is Result = 46 --> from Right to left . y++ * x happens first..So, 3 * 2 = 6 (now, y will be incremented to 4) then "Result = " +z (String) + number (y++ * z) will be concatenated as Strings.
x = 5;
System.out.println( x++*x); // output is 30 --> 5 * (5+1 i.e, x is already incremented to 6 when you do x++ so its like 5 *6 )
x = 5;
System.out.println( x*x++); // output is 25 -- > 5 * 5 (x will be incremented now)
回答2:
Something to understand:
The post-increment operator (++
after the variable name) returns the old value of the variable and then increments the variable. So, if x
is 5
, then the expression x++
evaluates to 5
and has the side effect that x
is set to 6
.
This one is a bit special:
x = 2; y = 3; z = 4;
System.out.println("Result = "+ z + y++ * x); // output is Result = 46
Note that string concatenation is being used here. It prints Result =
, then 4
which is the value of z
, then the value of y++ * x
which is 6
. The 46
is not one number, it is a 4
and a 6
put after each other coming from two expressions.
回答3:
postfix-++
means, the variable is evaluated at its current value and after the surrounding expression has been evaluated, the variable is incremented.
回答4:
y++ will add 1 to y after the code. ++y will add 1 to y before the code.
回答5:
They have higher precedence than the binary operators but they evaluate to 'x'. The side-effect of post-incrementing isn't part of the precedence.
回答6:
Because with y++
y
will be first be evaluated, then incremented.
Instead, with ++y
the increments happens before the evaluation.
回答7:
Your first expression z = y *= x++
is equal to this :
z=y=y*x;
x++;
Your second expression + z + y++ * x
is equivalent to this :
z + ""+ (y*x) // here z = 4 and y*x is 6 which gives us 46.
And similarly you can find out for 3rd and 4th expression.
回答8:
I suggest reading this tutorial, I think will shed some light on the usage -> Java operators.
Let's take it on pieces:
x = 5; y = 10;
System.out.println(z = y *= x++);
in the code above you have an assignment to z for the result of y * = x++; this means that y = y * x++. Now, x++ will evaluate after the multiplication is completed. If you wanted it to happen before, you should have used ++x.
x = 2; y = 3; z = 4;
System.out.println("Result = "+ z + y++ * x); // output is Result = 46
In this case you concatenate a string with the values above; the multiplication will be first, after that the addition and only in the end the post increment is evaluated.
The rest of the examples are similar to the ones above. The operator precedence isthe rule that applyes above, and you can use this table for seeing the order in which they are evaluated: Operator Precedence
来源:https://stackoverflow.com/questions/21905747/increment-and-decrement-operators-in-java