在 Java 中,当将参数传递给方法并修改传递的参数时,在方法调用期间是否保证结果是预期的?
例如
a.method(++i);
等
是否保证例如在method
中变量i
将更新
值(value)?
或者a.method(i++)
method
是在自增之后还是之前得到i
的值?
对于所有类似的情况也是一样的。
我记得这在 C++ 中是被禁止的,因为特定于实现,但也许我记错了。
最佳答案
前缀/后缀递增/递减运算符的 java 语言规范:
... the value 1 is added to the value of the variable and the sum is stored back into the variable ... The value of the prefix increment expression is the value of the variable after the new value is stored.
... the value 1 is added to the value of the variable and the sum is stored back into the variable ... The value of the postfix increment expression is the value of the variable before the new value is stored.
我觉得已经很清楚了。该函数将在前缀情况下获取增量值,而不是在后缀情况下。
关于java - 在方法调用期间修改参数时是否有任何保证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15074309/