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 ?
解决方案
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)