简体   繁体   English

Java中的增量和减量运算符

[英]Increment and Decrement operators in java

I had questions about incremental and decremental operators.I couldn't understand why java gave these outputs. 我对增量和减量运算符有疑问。我不明白为什么Java会给出这些输出。

    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. 例如,在第二个println函数中,y乘以而不增加1,而在第三个函数中,x函数与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(y ++ * x = 3 * 2 = 6)而为什么(y + 1)* x = 8呢?

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 . 因此,如果x5 ,则表达式x++计算结果为5并且副作用是x设置为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 . 它输出Result = ,然后输出4 ,即z的值,然后输出y++ * x的值6 The 46 is not one number, it is a 4 and a 6 put after each other coming from two expressions. 46不是一个数字,它是来自两个表达式的46

 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)

postfix- ++表示,该变量以其当前值求值,并且在对周围的表达式求值后,该变量递增。

y++ will add 1 to y after the code. y ++将在代码后向y加1。 ++y will add 1 to y before the code. ++ y将在代码之前将y加1。

They have higher precedence than the binary operators but they evaluate to 'x'. 它们的优先级高于二进制运算符,但它们的取值为'x'。 The side-effect of post-incrementing isn't part of the precedence. 后递增的副作用不是优先考虑的部分。

Because with y++ y will be first be evaluated, then incremented. 因为使用y++ y将首先被评估,然后被递增。

Instead, with ++y the increments happens before the evaluation. 相反,对于++y ,增量在评估之前发生。

Your first expression z = y *= x++ is equal to this : 您的第一个表达式z = y *= x++等于:

z=y=y*x;
x++;

Your second expression + z + y++ * x is equivalent to this : 您的第二个表达式+ z + y++ * x等效于此:

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. 同样,您可以找到第三和第四表达。

I suggest reading this tutorial, I think will shed some light on the usage -> Java operators. 我建议阅读本教程,我认为这将使用法-> Java运算符更加清晰

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++; 在上面的代码中,您将y * = x ++的结果分配给z; this means that y = y * x++. 这意味着y = y * x ++。 Now, x++ will evaluate after the multiplication is completed. 现在,x ++将在乘法完成后求值。 If you wanted it to happen before, you should have used ++x. 如果您希望它以前发生过,则应该使用++ 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 运算符优先级是上面应用的规则,您可以使用此表查看评估它们的顺序: 运算符优先级

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM