java 运算符 ++问题

发布于 2024-11-28 02:26:33 字数 327 浏览 0 评论 0原文

我想知道为什么第一个代码输出是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

卖梦商人 2024-12-05 02:26:33

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.

巨坚强 2024-12-05 02:26:33

增量运算符已经递增了 z,您不必将返回值分配回 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 to z.

z++

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.

脸赞 2024-12-05 02:26:33

当您使用后自增运算符时,不需要将结果分配回变量。

也就是说,您的代码应如下所示:

int z=0;
    while(z<4)
    {
       ++z;
       System.out.print(z);

    }

在 Java 中,操作在增量之前返回 z 的值(随后在幕后增量变量),然后将该值重新分配给z。这就是为什么它永远不会改变。

预增量运算符将执行增量并返回新结果,因此您将得到您所期望的结果:

int z=0;
    while(z<4)
    {
       z=++z;
       System.out.print(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:

int z=0;
    while(z<4)
    {
       ++z;
       System.out.print(z);

    }

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 to z. 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:

int z=0;
    while(z<4)
    {
       z=++z;
       System.out.print(z);

    }

This will print 1234.

九厘米的零° 2024-12-05 02:26:33

请记住这一点,Java 从右到左计算表达式(就像 C 和 C++ 一样),

因此,如果您的代码

z = z++

在执行此行之前读取 then if z is 0,则会发生以下情况:

  1. z++< /code> 作为表达式求值,返回值 0
  2. 然后,由于 ++ 运算符,z 会递增,并且其值为 1。
  3. 现在,z 上left 被分配了一个值 z = (返回的值z++)
  4. 由于 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

z = z++

then if z is 0 before this line is executed, what happens is:

  1. z++ is evaluated as an expression, returning the value 0
  2. Then z is incremented because of the ++ operator, and it has the value 1.
  3. Now the z on the left is assigned a value z = (value returned by z++)
  4. Since the value returned by 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 the z variable on the left is updated.

携君以终年 2024-12-05 02:26:33

我想这会给你一个很好的解释。

考虑这个类:

public class T
{
    public void f() {
    int count = 0;
    count = count++;
    }
}

这是关联的字节代码:

public void f();
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   iinc    1, 1
   6:   istore_1
   7:   return
}
  1. iconst_0 将常量 0 加载到堆栈上(这是为了将值 0 分配给变量 count >
  2. istore_1 将堆栈值(现在为 0)存储到变量 1
  3. iload_1 从变量 1 加载 int 值(0现在)入栈
  4. zinc 1, 1 增加 1 变量 1(现在 count = 1
  5. istore_1 存储堆栈值(< code>0 现在从步骤 #3) 到变量 1
  6. return

现在应该非常清楚 count = count++ 是如何在 Java 中编译的。

I think this will give you a pretty good explanation.

Consider this class:

public class T
{
    public void f() {
    int count = 0;
    count = count++;
    }
}

This is the associated byte code:

public void f();
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   iinc    1, 1
   6:   istore_1
   7:   return
}
  1. iconst_0 loads the constant 0 onto the stack (this is for assigning the variable count with value 0
  2. istore_1 stores stack value (0 right now) into variable 1
  3. iload_1 loads int value from variable 1 (0 right now) onto the stack
  4. zinc 1, 1 increments by 1 variable 1 (count = 1 right now)
  5. istore_1 stores stack value (0 right now from step #3) into variable 1
  6. return

Now it should be pretty clear how count = count++ gets compiled in Java.

×纯※雪 2024-12-05 02:26:33

这是因为您使用后缀运算符分配 z 的值。

http://download.oracle.com/javase/tutorial/java/nutsandbolts /operators.html

int z = 0;
i = z++; // i equals 0
x = ++z; // x equals 2

后缀运算符将在分配 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

int z = 0;
i = z++; // i equals 0
x = ++z; // x equals 2

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.

惟欲睡 2024-12-05 02:26:33

第一个可能更好地写为

int z=0;
while(z++<4)
{
   System.out.print(z);
}

or

int z=0;
while(z<4)
{
   z = ++z;
   System.out.print(z);
}

这里的预自增很重要,因为它会先递增然后分配。而不是分配然后递增 - 除了在第一个示例中重置为 0 之外没有任何效果。

因为当您执行 z=z++ 时,它会将旧值重新分配回 z ,从而导致无限循环。

第二个将结束,因为您没有重新分配回 z:

int z=0;
int x=0;

while(z<5)
{
    x=z++;
    System.out.print(x);
}

这将打印 1234。

The first one is probably better written as

int z=0;
while(z++<4)
{
   System.out.print(z);
}

or

int z=0;
while(z<4)
{
   z = ++z;
   System.out.print(z);
}

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 to z thus leading to an infinite loop.

The second one will end because you are not reassigning back to z:

int z=0;
int x=0;

while(z<5)
{
    x=z++;
    System.out.print(x);
}

This will print 1234.

月下客 2024-12-05 02:26:33

如果您编写类似 foo = foo++ 的内容,那么您就错了。一般来说,如果您看到任何类似 x = x++ + ++x; 的表达式,则说明存在严重错误。无法预测此类表达式的计算方式。在像C这样的语言中,可以根据实现者的需要来计算此类表达式。

我强烈建议使用 ++ 运算符,因为您在阅读代码时一定会遇到它。

正如其他人指出的,x++ 是后缀运算符,++x 是前缀运算符。

int x = 0;
int y = x++; // y=0, x=1
int z = ++x; // z=2, x=2

请注意,yzx 的值只是表达式求值之后的值。它们在执行期间的内容是未定义的。

因此,如果您看到类似 foo(x++, ++x, x) 的代码,请向山上跑。

您自己的问题写得更简洁:

for (int z=0; z<4; ++z) {
    System.out.print(z);
}

上面的代码的优点是变量 z 的范围在 for 循环内,因此它不会意外地与其他变量发生冲突。

If you're writing anything like foo = foo++, you're doing it wrong. In general, if you see any expression like x = x++ + ++x; something is seriously wrong. It's impossible to predict how expressions of that sort are evaluated. In languages like C, 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.

int x = 0;
int y = x++; // y=0, x=1
int z = ++x; // z=2, x=2

Note that the values of y, z and x 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:

for (int z=0; z<4; ++z) {
    System.out.print(z);
}

The above code has the advantage that the variable z is scoped within the for loop, so it won't accidentally collide with some other variable.

薆情海 2024-12-05 02:26:33
z=z++;

这意味着首先将 z(位于右侧位置)的值分配给 z(位于左侧位置),然后对右侧 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文