java java中的++运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10665571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:01:15  来源:igfitidea点击:

++ operator in java

javaoperators

提问by freshDroid

Possible Duplicate:
java operator ++ problem

可能重复:
java operator ++ 问题

This is the code. I know the difference between c++ and ++c.

这是代码。我知道 c++ 和 ++c 之间的区别。

public class sample {
  public static void main(String[] b){
    int count = 0,a=0;
    for (int i = 0; i < 3; i++){
      count=count++;
      System.out.println(count);
    }

But what I am expecting is

但我期待的是

count=count;count=count+1;//output has to be 1  2 3

but the output is 0 0 0.

但输出是 0 0 0。

回答by Martin.

Your prediction is wrong.

你的预测是错误的。

count++ will increment count by 1 and return the old value (0). Which is your case. Afterwards, you assign the old value (0) to your countvariable. To make it more understandable, just look at this code

count++ 会将 count 增加 1 并返回旧值 (0)。这是你的情况。之后,您将旧值 (0) 分配给您的count变量。为了使它更容易理解,只需看一下这段代码

count = count; // is the same as count = count++;

Don't use count = count++;, just use count++

不使用count = count++;,只使用count++

回答by MByD

In Java, this code is guaranteed to keep the variable with the same value.

在 Java 中,此代码保证保持变量具有相同的值。

It's like:

就像是:

int temp;
temp = count;
count = count +1;
count = temp;

to acheieve what you want, write:

实现你想要的,写:

count++; //or
count += 1; //or
count = count +1;

回答by Radu Murzea

Replace count=count++;with count++;.

替换count=count++;count++;

回答by Armen Tsirunyan

count = count ++;

Here's what is happening.

这就是正在发生的事情。

First, count++is evaluated, which evaluated to 0, but increments count. And this 0is assigned to count. So count remains 0. The following is different, because ++count evaluates to 1, 2...

首先,count++被评估,评估为 0,但递增count。这0被分配给计数。所以 count 仍然是 0。 下面是不同的,因为 ++count 的计算结果是 1, 2...

count = ++count;

回答by Adhithya

I slightly modified your code and I have made it to work

我稍微修改了你的代码,我已经让它工作了

public class sample {
    public static void main(String[] b){
        int count = 0,a=0;
        for (int i = 0; i < 3; i++){
            count++;
            System.out.println(count);
        }
    }
}

You don't have to reassign the value of count++ to count. Java will do it for you. I added some parentheses which were missing in your code. I hope this helps.

您不必将 count++ 的值重新分配给 count。Java 将为您完成。我添加了一些您的代码中缺少的括号。我希望这有帮助。

回答by Pablo Santa Cruz

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_0loads the constant 0 onto the stack (this is for assigning the variable countwith value 0
  2. istore_1stores stack value (0right now) into variable 1
  3. iload_1loads int value from variable 1 (0right now) onto the stack
  4. zinc 1, 1increments by 1variable 1 (count = 1right now)
  5. istore_1stores stack value (0right now from step #3) into variable 1
  6. return
  1. iconst_0将常量 0 加载到堆栈上(这是为了给变量count赋值0
  2. istore_1将堆栈值(0现在)存储到变量 1 中
  3. iload_1将变量 1(0现在)中的int 值加载到堆栈中
  4. zinc 1, 11变量 1递增(count = 1现在)
  5. istore_1将堆栈值(0现在从第 3 步开始)存储到变量 1 中
  6. 返回

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

现在应该很清楚如何count = count++在 Java 中编译了。