gpt4 book ai didi

java - 为什么 `index = index++` 不增加 `index` ?

转载 作者:行者123 更新时间:2023-12-01 07:00:09 26 4
gpt4 key购买 nike

** 重复:What's the difference between X = X++; vs X++;? **

所以,即使我知道你永远不会在代码中真正做到这一点,我仍然很好奇:

public static void main(String[] args) {
int index = 0;
System.out.println(index); // 0
index++;
System.out.println(index); // 1
index = index++;
System.out.println(index); // 1
System.out.println(index++); // 1
System.out.println(index); // 2
}

请注意第三个 sysout仍然是1 。在我看来,这行index = index++;意思是“将索引设置为索引,然后将索引增加1”,以同样的方式 System.out.println(index++);意思是“将索引传递给 println 方法,然后将索引增加 1”。

然而事实并非如此。谁能解释一下这是怎么回事?

最佳答案

这是一个重复的问题。

编辑:我似乎找不到原文:P哦好吧

a = a++ 使用后增量,编译器将其解释为:

a = function() {
var old_value = a;
a++;
return old_value;
}

编辑2: What's the difference between X = X++; vs X++;?

关于java - 为什么 `index = index++` 不增加 `index` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/354393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com