后增量运算符java [英] post increment operator java

查看:25
本文介绍了后增量运算符java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从 joshua bloch 的Java 益智游戏"中找出以下代码的正面或反面.

公共类Test22{公共静态无效主(字符串参数[]){int j=0;for(int i=0;i<100;i++){j=j++;}System.out.println(j);//打印0int a=0,b=0;a=b++;System.out.println(a);System.out.println(b);//打印1}}

我无法得到 j 打印 0 的部分.据作者介绍,

j=j++

类似于

temp=j;j=j+1;j=温度;

但是

a=b++

使 b 为 1.所以它应该像这样评估,

a=bb=b+1

按照同样的逻辑,不应该

j=j++

被评估为,

j=jj=j+1

这里的温度在哪里出现?任何解释将不胜感激.<<我正在为这个头疼.;)>>提前致谢.

解决方案

让我们分解你自己的论点:

<块引用>

据作者说,

j=j++;

类似于

temp=j;j=j+1;//增量j=温度;//然后赋值

是的,到目前为止你是对的......但你错了:

<块引用>

但是

a=b++;

使 b=1.所以它应该像这样评估,

a=b;//分配b=b+1;//然后递增

错了!您没有始终如一地应用规则!您已将顺序从增加然后分配更改为分配然后增加!!!它实际上是这样评估的:

temp=b;b=b+1;//增量a=温度;//然后赋值

基本上是这种形式的赋值:

lhs = rhs++;

类似于做这样的事情:

temp = rhs;rhs = rhs+1;//增量lhs = 温度;//然后赋值

将此应用于a = b++;.然后将它也应用到 j = j++;.这就是为什么你会得到你得到的结果.

你所做的是你对a = b++;所做的事情有自己的解释——一个不符合上述规则的错误解释.这就是你困惑的根源.

<小时>

另见

  • JLS 15.14.2 后缀增量运算符<块引用>

    "...将值 1 与变量的值相加,并将和存储回变量中 [...] 后缀增量表达式的值是变量的值 before 存储新值."

I can't make heads or tails of the following code from "java puzzlers" by joshua bloch.

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=j++

is similar to

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

But

a=b++

makes b 1. So it should've evaluated like this,

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. 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++;. Then apply it also to 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++; 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

    "...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."

这篇关于后增量运算符java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆