java 运算符 ++问题
我想知道为什么第一个代码输出是 000 而第二个代码输出是 123
第一个:
int z=0;
while(z<4)
{
z=z++;
System.out.print(z);
}
第二个:
int z=0;
int x=0;
while(z<5)
{
x=z++;
System.out.print(x);
}
这两个代码有什么不同,为什么第一个块不增加 z 的值?
I wonder why first code output is 000 while the second one is 123
first one:
int z=0;
while(z<4)
{
z=z++;
System.out.print(z);
}
second one :
int z=0;
int x=0;
while(z<5)
{
x=z++;
System.out.print(x);
}
what is the different between these two codes , why the first block do not increase the value of the z ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
z=z++
是程序员的错误 - 它所做的是递增 z,然后将 z 设置为其旧值 - 结果它用旧值覆盖 z 并因此撤消增量。z=z++
is a programmer's error -- what it does is increment z and then set z to its old value -- as a result it overwrites z with its old value and hence undoes the increment.增量运算符已经递增了
z
,您不必将返回值分配回z
。是后期增量。它返回 z 并在增加 z 之后。
在第一个示例中,您基本上只是将 0 分配给 z 并且循环不应该结束。
在第二个示例中,您将 z 的旧值分配给 x,然后递增 z。这意味着您不会像第一个示例中那样再次开始递增 0,但是当 z 达到 5(因此 z<5 为 false)时,由于后递增,z 为 5,x 为 4。
The increment operator already increments
z
, you don't have to assign the return value back toz
.Is a post increment. It returns z and AFTER that it increments z.
In your first sample, you are basically just assigning 0 to z and your loop shouldn't end.
In your second sample, you are assigning the old value of z to x and then increment z. This means that you don't start to increment 0 again like in the first example, but when z reaches 5 (so z<5 is false), z is 5 and x is 4 because of the post increment.
当您使用后自增运算符时,不需要将结果分配回变量。
也就是说,您的代码应如下所示:
在 Java 中,操作在增量之前返回 z 的值(随后在幕后增量变量),然后将该值重新分配给
z
。这就是为什么它永远不会改变。预增量运算符将执行增量并返回新结果,因此您将得到您所期望的结果:
这将打印
1234
。When you use the post-increment operator, you don't need to assign the result back to the variable.
That is, your code should look like this:
In Java, the operation returns the value of
z
BEFORE the increment (while incrementing the variable behind the scenes afterwards), and that value is then RE-assigned toz
. That's why it never changes.The pre-increment operator will do the increment and return the NEW result, so you'll get what you expect:
This will print
1234
.请记住这一点,Java 从右到左计算表达式(就像 C 和 C++ 一样),
因此,如果您的代码
在执行此行之前读取 then if z is 0,则会发生以下情况:
z++< /code> 作为表达式求值,返回值 0
z
会递增,并且其值为 1。z
上left 被分配了一个值z = (返回的值z++)
z++
返回的值为 0,因此z
被重置为 0。需要注意的重要一点是
z++
中固有的赋值结果code>z++ 在更新左侧的z
变量之前进行计算。Remember this, Java evaluates your expressions right to left (just like C and C++),
So if your code reads
then if z is 0 before this line is executed, what happens is:
z++
is evaluated as an expression, returning the value 0z
is incremented because of the ++ operator, and it has the value 1.z
on the left is assigned a valuez = (value returned by z++)
z++
was 0,z
is reset to 0.The important thing to note is that the result of the assignment inherent in
z++
is evaluated before thez
variable on the left is updated.我想这会给你一个很好的解释。
考虑这个类:
这是关联的字节代码:
iconst_0
将常量 0 加载到堆栈上(这是为了将值0
分配给变量count
>istore_1
将堆栈值(现在为0
)存储到变量 1iload_1
从变量 1 加载 int 值(0
现在)入栈zinc 1, 1
增加1
变量 1(现在count = 1
)istore_1
存储堆栈值(< code>0 现在从步骤 #3) 到变量 1现在应该非常清楚
count = count++
是如何在 Java 中编译的。I think this will give you a pretty good explanation.
Consider this class:
This is the associated byte code:
iconst_0
loads the constant 0 onto the stack (this is for assigning the variablecount
with value0
istore_1
stores stack value (0
right now) into variable 1iload_1
loads int value from variable 1 (0
right now) onto the stackzinc 1, 1
increments by1
variable 1 (count = 1
right now)istore_1
stores stack value (0
right now from step #3) into variable 1Now it should be pretty clear how
count = count++
gets compiled in Java.这是因为您使用后缀运算符分配 z 的值。
http://download.oracle.com/javase/tutorial/java/nutsandbolts /operators.html
后缀运算符将在分配 i 后增加 z 的值。
一元运算符
++
将在赋值x之前增加z的值。将其视为 z 之前的
++
作为赋值前的 +1,将 z 之后的++
视为赋值后的 +1。It's because you are assigning the value of z with a postfix operator.
http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Postfix operators will increment the value of z after assignment of i.
Unary operator
++
will increment the value of z before assignment of x.Think of it as
++
before z as +1 before assignment,++
after z as +1 after assignment.第一个可能更好地写为
or
这里的预自增很重要,因为它会先递增然后分配。而不是分配然后递增 - 除了在第一个示例中重置为 0 之外没有任何效果。
因为当您执行
z=z++
时,它会将旧值重新分配回z
,从而导致无限循环。第二个将结束,因为您没有重新分配回 z:
这将打印 1234。
The first one is probably better written as
or
The pre-increment here is important because it will increment and then assign. Rather than assign and then increment - which has no effect other than resetting to 0 in your first example.
Since when you do
z=z++
it will reassign the old value back toz
thus leading to an infinite loop.The second one will end because you are not reassigning back to z:
This will print 1234.
如果您编写类似
foo = foo++
的内容,那么您就错了。一般来说,如果您看到任何类似x = x++ + ++x;
的表达式,则说明存在严重错误。无法预测此类表达式的计算方式。在像C
这样的语言中,可以根据实现者的需要来计算此类表达式。我强烈建议使用
++
运算符,因为您在阅读代码时一定会遇到它。正如其他人指出的,
x++
是后缀运算符,++x
是前缀运算符。请注意,
y
、z
和x
的值只是表达式求值之后的值。它们在执行期间的内容是未定义的。因此,如果您看到类似
foo(x++, ++x, x)
的代码,请向山上跑。您自己的问题写得更简洁:
上面的代码的优点是变量
z
的范围在for
循环内,因此它不会意外地与其他变量发生冲突。If you're writing anything like
foo = foo++
, you're doing it wrong. In general, if you see any expression likex = x++ + ++x;
something is seriously wrong. It's impossible to predict how expressions of that sort are evaluated. In languages likeC
, such expressions can be evaluated as the implementer desires.I'd strongly recommend playing around with the
++
operator because you're bound to encounter it when you read code.As others have pointed out,
x++
is the postfix operator and++x
is a prefix operator.Note that the values of
y
,z
andx
are what they are only after the expression is evaluated. What they are during execution is undefined.So if you see code like
foo(x++, ++x, x)
, run for the hills.Your own problem is more succinctly written:
The above code has the advantage that the variable
z
is scoped within thefor
loop, so it won't accidentally collide with some other variable.这意味着首先将 z(位于右侧位置)的值分配给 z(位于左侧位置),然后对右侧 z 进行增量(这是没有用的)。
This means first assign the value of z (which is in right position) to the z (which in left position), then do the increment in right z (which is of no use).