增量逻辑 [英] Incrementor logic
问题描述
我正在尝试更深入地使用post和pre incrementors,但我对以下表达式有点困惑:
I'm trying to get deeper with post and pre incrementors but am a bit stuck with the following expression :
public static void main(String[] args) {
int i = 0;
i = i+=(++i + (i+=2 + --i) - ++i);
// i = 0 + (++i + (i+=2 + --i) - ++i);
// i = 0 + (1 + (3 + 2) - 1);
// i = 0 + (6 - 1);
System.out.println(i); // Prints 0 instead of 5
}
我知道我错过了某处的逻辑但是在哪里?
I know I'm missing the logic somewhere but where?
我尝试了什么:
- 从左到右对(虽然我知道不建议这样做)
- 从最里面开始,从那里开始。
感谢您的帮助
PS:评论是我的微积分的详细信息
PS : The comments are the details of my calculus
编辑1
我试图将 2
中的de hard编码值更改为某些内容否则结果总是给出 0
I tried to change de hard coded value from the expression from 2
to something else and the result always gives 0
看看这个例子:
int i = 0;
i = i+=(++i + (i+=32500 + --i) - ++i);
System.out.println(i); // Prints 0
此表达式在逻辑上应该远不及 0
但不知何故它确实打印出来。
This expression should logically be nowhere near 0
but somehow it does print it.
当我使用否定时也是如此:
The same happens when I use a negative :
int i = 0;
i = i+=(++i + (i+=(-32650) + --i) - ++i);
System.out.println(i); // Prints 0
编辑2
现在,我将 i
的值改为:
int i = 1;
i = i+=(++i + (i+=2 + --i) - ++i);
System.out.println(i); // Prints 2
i = 2;
i = i+=(++i + (i+=10000 + --i) - ++i);
System.out.println(i); // Prints 4
i = 3;
i = i+=(++i + (i+=(-32650) + --i) - ++i);
System.out.println(i); // Prints 6
它给出了的两倍i
每次,无论硬编码值是什么。
It gives the double of i
each time, whatever the hard coded value is.
推荐答案
Java编程语言保证运算符的操作数似乎以特定的评估顺序进行评估,即从从左到右。
在评估右侧操作数的任何部分之前,二元运算符的左侧操作数似乎完全评估。
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
如果运营商是复合赋值运算符(§15.26.2),然后评估左手操作数包括记住变量即左侧操作数表示并获取并保存该变量的值,以便在隐含的二进制操作中使用。
If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable's value for use in the implied binary operation.
所以,基本上, i + = ++ i
会记住左侧的 i
的旧值,在评估右侧之前。
So, essentially, i += ++i
will remember the old value of i
on the left side, before evaluating the right side.
请记住,操作数的评估顺序和运算符的优先级是两件不同的事情。
Remember, evaluation order of operands and precedence of operators are two different things.
逐步显示评估顺序,并在{braces}中保存值:
Showing evaluation order, step by step, with saved value in {braces}:
int i = 0;
i = i += (++i + (i += 2 + --i) - ++i); // i = 0
i{0} = i += (++i + (i += 2 + --i) - ++i); // i = 0
i{0} = i{0} += (++i + (i += 2 + --i) - ++i); // i = 0
i{0} = i{0} += (1 + (i += 2 + --i) - ++i); // i = 1
i{0} = i{0} += (1 + (i{1} += 2 + --i) - ++i); // i = 1
i{0} = i{0} += (1 + (i{1} += 2 + 0 ) - ++i); // i = 0
i{0} = i{0} += (1 + (i{1} += 2 ) - ++i); // i = 0
i{0} = i{0} += (1 + 3 - ++i); // i = 3
i{0} = i{0} += (4 - ++i); // i = 3
i{0} = i{0} += (4 - 4 ); // i = 4
i{0} = i{0} += 0 ; // i = 4
i{0} = 0 ; // i = 0
0 ; // i = 0
跟进编辑问题
如果我们将初始值命名为 I
,则常量 N
:
If we name the initial value I
and the constant N
:
int i = I;
i = i += (++i + (i += N + --i) - ++i);
然后我们可以看到价值是:
Then we can see that the values are:
i{I} = i{I} += ((I+1) + (i{I+1} += N + I) - ((I+1+N+I)+1));
i{I} = i{I} += (I + 1 + (I + 1 + N + I) - (I + 1 + N + I + 1));
i{I} = i{I} += (I + 1 + I + 1 + N + I - I - 1 - N - I - 1);
i{I} = i{I} += I;
i{I} = I + I;
i = 2 * I;
这篇关于增量逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!