后置和前置增量运算符
当我运行以下示例时,我得到输出 0,2,1
class ZiggyTest2{
static int f1(int i) {
System.out.print(i + ",");
return 0;
}
public static void main(String[] args) {
int i = 0;
int j = 0;
j = i++; //After this statement j=0 i=1
j = j + f1(j); //After this statement j=0 i=1
i = i++ + f1(i); //i++ means i is now 2. The call f1(2) prints 2 but returns 0 so i=2 and j=0
System.out.println(i); //prints 2?
}
}
我不明白为什么输出是 0,2,1 而不是 0,2,2
When i run the following example i get the output 0,2,1
class ZiggyTest2{
static int f1(int i) {
System.out.print(i + ",");
return 0;
}
public static void main(String[] args) {
int i = 0;
int j = 0;
j = i++; //After this statement j=0 i=1
j = j + f1(j); //After this statement j=0 i=1
i = i++ + f1(i); //i++ means i is now 2. The call f1(2) prints 2 but returns 0 so i=2 and j=0
System.out.println(i); //prints 2?
}
}
I don't understand why the output is 0,2,1 and not 0,2,2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果我们展开
i = i++ + f1(i)
语句,我们会得到类似下面的内容,我想主要步骤可以像上面那样总结。
If we expand
i = i++ + f1(i)
statement, we get something like followingI guess main steps can be summarized like above.
i++
表示i
现在是2
。调用f1(i)
打印2
但返回 0,因此i=2
和j=0
在此 < code>i = 1 ,现在想象一下
f1()
被调用并替换为 0所以
现在它会
用更简单的话来说(来自 此处 @ Piotr )
另一个这样的示例:
i++
meansi
is now2
. The callf1(i)
prints2
but returns 0 soi=2
andj=0
before this
i = 1
, now imaginef1()
called and replaced with 0so
now it would be
In Simpler words (from here @ Piotr )
Another such Example :
从这个例子可以理解解决方案
因此从这一行来看,
你的 i 仍然是 1,显然该函数将返回 0。它再次存储在 i 中,因此值 1。而不是 i 的更新值存储在i,您正在通过赋值运算符覆盖它。
The solution can be understood from this example
Hence from this line,
Your i is still 1 and obviously the function is going to return 0. Which is again stored in i and hence the value 1. Instead of the updated value of i being stored in i, you are overriding it by the assignment operator.
在后增量运算符中,操作数的值将在使用后增加。
示例
第一个 k(值 = 1)被赋值为 l,之后 k 值将增加
类似的事情发生在下面的行中
In Post increment operator value will of operand will increase after use.
Example
First k (value = 1) is assigned with l after that the k value will increase
Similarly thing happens in the following line
预自增是指:变量加一,返回增量后的值;
后自增——先返回i,然后自增;
Pre increment means: add one to variable and return incremented value;
Post increment - first return i, then increment it;
要深入了解,您需要查看 表达式 及其求值顺序
这里对方程 i++ + f1(i) 求值有一些解释
在方程编译器中得到 "i" 等于 1并穿上它堆栈为第一个操作数,然后增量“i”,因此其值将为2,并通过以下方式计算第二个操作数调用函数将为0,并且在操作(+)时执行操作数将为1和0 。
To get In-depth you need to see Expression and its Evaluation Order
Here's little explanation about equation i++ + f1(i) evaluation
In Equation Compiler get "i" which is Equals to 1 and put it on stack as first operand then increments "i", so its value would be 2, and calculates second operand by calling function which would be 0 and at the time of operation (+) execution operands would be 1 and 0.
希望这个解释可能有一些帮助:
请尝试这个
所以简而言之,在后增量期间,首先解决表达式,然后增加值。但在预增量中,首先对值进行增量,然后求解表达式。
但如果你只写
那么两者的意思是一样的。
问候
Hope this explaination might be of some help :
Do try this
So in short during post increment, expression is first solved and then value is incremented. But in pre increment, value is first incremented and then expression is solved.
But if you write only
Then both means the same thing.
Regards