简体   繁体   English

post increment operator java

[英]post increment operator java

I can't make heads or tails of the following code from "java puzzlers" by joshua bloch. 我无法通过joshua bloch的“java puzzlers”来制作以下代码的正面或反面。

public class Test22{
 public static void main(String args[]){
  int j=0;
  for(int i=0;i<100;i++){ 
    j=j++;
  }
  System.out.println(j); //prints 0

  int a=0,b=0;
  a=b++;
  System.out.println(a);
  System.out.println(b); //prints 1


 }
}

I can't get the part where j prints 0. According to the author, 我不能得到j打印0的部分。据作者说,

j=j++

is similar to 类似于

temp=j;
j=j+1;
j=temp;

But

a=b++

makes b 1. So it should've evaluated like this, 制作b 1.所以它应该像这样评估,

a=b
b=b+1

By following the same logic, shouldn't 遵循相同的逻辑,不应该

j=j++

be evaluated as, 被评估为,

j=j
j=j+1

Where does the temp come into picture here? 这里的温度在哪里? Any explanations would be much appreciated. 任何解释都将非常感激。 << I'm breaking my head over this. “我对此深有所怀疑。 ;)>> Thanks in advance. ;)>>提前致谢。

Let's break down your own argument: 让我们分解你自己的论点:

According to the author, 据作者说,

 j=j++; 

is similar to 类似于

 temp=j; j=j+1; // increment j=temp; // then assign 

Yes, you're right so far..., but here's where you got it wrong: 是的,到目前为止你是对的...,但这里是你弄错了:

But

 a=b++; 

makes b=1 . 使b=1 So it should've evaluated like this, 所以它应该像这样评估,

 a=b; // assign b=b+1; // then increment 

WRONG! 错误! You're not applying the rule consistently! 你没有一贯地应用这条规则! You've changed the order from increment then assign to assign then increment !!! 你已经从增量改变了顺序, 然后分配分配再增加 !!! It's actually evaluated like this: 它实际上是这样评估的:

temp=b;
b=b+1;     // increment
a=temp;    // then assign

Basically assignments of this form: 基本上这种形式的作业:

lhs = rhs++;

is similar to doing something like this: 做类似于这样的事情:

temp = rhs;
rhs = rhs+1;  // increment
lhs = temp;   // then assign

Apply this to a = b++; 将此应用于a = b++; . Then apply it also to j = j++; 然后将它也应用于j = j++; . That's why you get the results that you get. 这就是为什么你得到你得到的结果。

What you did was you came up with your own interpretation of what a = b++; 你做了什么, 你想出了自己对 a = b++; 的解释 a = b++; does -- a WRONG interpretation that doesn't follow the above rule. 确实 - 不符合上述规则的错误解释。 That's the source of your confusion. 这是你困惑的根源。


See also 也可以看看

  • JLS 15.14.2 Postfix Increment Operator JLS 15.14.2后缀增量运算符

    "...the value 1 is added to the value of the variable and the sum is stored back into the variable [...] The value of the postfix increment expression is the value of the variable before the new value is stored." “...将值1添加到变量的值中,并将总和存储回变量[...]后缀增量表达式的值是存储新值之前变量的值。”

The post increment operator implicitly uses a temp variable. 后增量运算符隐式使用临时变量。 This allows it to return one value while setting its argument to another. 这允许它在将参数设置为另一个时返回一个值。 That's why 这就是为什么

a = b++;

Can increment b , but set a to the old value of b . 可以递增b ,但将a设置为b旧值 The same thing is going on with 同样的事情正在继续

j = j++;

The variable is incremented on the right hand side, but it's then set back to the old value when the assignment takes place. 变量在右侧递增,但在赋值发生时将其设置回旧值。

j++ will use the old value of j and then it will increment it. j++将使用旧值j ,然后它会增加它。 But when it overwrites the left hand side, it will use the old value of j. 但是当它覆盖左侧时,它将使用旧的j值。

It is similar to : 它类似于:

temp=j;
j += 1; 
j=temp;     // take the old value of j.

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

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