++;java中的运算符

++;java中的运算符,java,operators,Java,Operators,可能重复: 这是代码。我知道C++和+c.的区别。p> 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); } 但是输出是0 你的预测是错误的 计数++将计数增加1,并返回旧值(0)。这是你的

可能重复:

这是代码。我知道C++和+c.的区别。p>
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);
    }

但是输出是0

你的预测是错误的

计数++将计数增加1,并返回旧值(0)。这是你的情况。然后,将旧值(0)分配给
count
变量。为了让它更容易理解,只需看看下面的代码

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

不要使用
count=count++,只需使用
count++

您的预测是错误的

计数++将计数增加1,并返回旧值(0)。这是你的情况。然后,将旧值(0)分配给
count
变量。为了让它更容易理解,只需看看下面的代码

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

不要使用
count=count++,只需在Java中使用
count++

,此代码保证保持变量具有相同的值

就像:

int temp;
temp = count;
count = count +1;
count = temp;
要实现您想要的,请写:

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

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

就像:

int temp;
temp = count;
count = count +1;
count = temp;
要实现您想要的,请写:

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

替换
count=count++带有
计数++

替换
count=count++带有
计数++

count = count ++;
下面是正在发生的事情

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

count = ++count;
下面是正在发生的事情

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

count = ++count;

我稍微修改了你的代码,我已经成功了

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);
        }
    }
}
公共类示例{
公共静态void main(字符串[]b){
整数计数=0,a=0;
对于(int i=0;i<3;i++){
计数++;
系统输出打印项次(计数);
}
}
}

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

我稍微修改了您的代码,并使其正常工作

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);
        }
    }
}
公共类示例{
公共静态void main(字符串[]b){
整数计数=0,a=0;
对于(int i=0;i<3;i++){
计数++;
系统输出打印项次(计数);
}
}
}

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

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

以这一类为例:

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
}
  • iconst_0
    将常量0加载到堆栈中(这用于为变量
    count
    赋值
    0
  • istore_1
    将堆栈值(
    0
    现在)存储到变量1中
  • iload_1
    将int值从变量1(
    0
    现在)加载到堆栈上
  • 1,1
    增加变量1(
    count=1
    现在)
  • istore_1
    将堆栈值(
    0
    现在从步骤#3)存储到变量1中
  • 返回

  • 现在应该很清楚如何用Java编译
    count=count++

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

    以这一类为例:

    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
    }
    
  • iconst_0
    将常量0加载到堆栈中(这用于为变量
    count
    赋值
    0
  • istore_1
    将堆栈值(
    0
    现在)存储到变量1中
  • iload_1
    将int值从变量1(
    0
    现在)加载到堆栈上
  • 1,1
    增加变量1(
    count=1
    现在)
  • istore_1
    将堆栈值(
    0
    现在从步骤#3)存储到变量1中
  • 返回

  • 现在应该很清楚
    count=count++
    是如何用Java编译的。

    +1哎哟,这是一件令人讨厌的事情。我试图理解那些不用于实现的概念…+1哎哟,这是一件令人讨厌的事情。我试图理解那些不用于实现的概念…@freshdroid这就是你想要的。所以我n c=a++和c=++都将首先执行增量。但区别是c将在第一种情况下被赋予旧值,在第二种情况下被赋予更新值..我说得对吗?@freshdroid这就是你所寻求的。所以在c=a++和c=++中,增量都将首先执行。但区别是c将在第一种情况下被赋予旧值,在第二种情况下被赋予更新值..我准备好了吗ht?