Java中前缀和后缀++运算符之间的区别 [英] Difference between prefix and postfix ++ operators in Java

查看:357
本文介绍了Java中前缀和后缀++运算符之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此有一些疑问(例如 Java:增/减运算符?),但我不是不是,而是询问后缀和前缀++运算符之间的一般区别(我知道那部分),而是关于基本区别它们在Java规范级别.

There are a few questions regarding this (like Java: Prefix/postfix of increment/decrement operators?) but I'm not asking about the general difference between postfix and prefix ++ operators (I know that part), but about the fundamental difference between them at the Java specification level.

具体来说,前缀和后缀++运算符 other 不同于运算符优先级(可能是javac将命令转换为字节码的方式,还是JVM运行该字节码的方式) ?

Specifically, is there any difference between prefix and postfix ++ operators other than operator precedence (maybe in the way the javac translates the commands to bytecode or in the way the JVM runs that bytecode)?

例如,以下代码是否会必要(在每个JVM中)运行相同的代码?

For example, would the following code necessarily run the same (in every JVM):

for (int i = 0; i < X; i++) { ... }

还有

for (int i = 0; i < X; ++i) { ... }

JLS中是否有定义这2条语句在每个平台,Java编译器,JVM等上以完全相同的方式运行的东西,或者(理论上来说)这2条语句将以不同的方式运行?

Is there anything in the JLS that defines that these 2 statements would run in exactly the same way on every platform, Java compiler, JVM, etc., or is it possible (even theoretically) that these 2 statements will run differently?

推荐答案

规范的相关部分是:

15.1.5.1.前缀增量运算符++

[…]

15.15.1. Prefix Increment Operator ++

[…]

在运行时,如果对操作数表达式的求值突然完成,则前缀增量表达式由于相同的原因而突然完成,并且不会发生增量.否则,将值1加到变量的值上,然后将总和存储回变量中. […]
前缀增量表达式的值是存储新值之后的变量 的值.

At run time, if evaluation of the operand expression completes abruptly, then the prefix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, 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.

15.1.4.2. Postfix增量运算符++

[…]

15.14.2. Postfix Increment Operator ++

[…]

在运行时,如果操作数表达式的求值突然完成,则后缀增量表达式由于相同的原因而突然完成,并且不会发生增量.否则,将值1加到变量的值上,然后将总和存储回变量中. […]
后缀增量表达式的值是存储新值的变量之前的值.

At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, 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.

因此,差异是在表达式上下文中使用时的结果值.在for循环的update子句的上下文中使用它时,相关的部分是:

So, the only difference is the result value when used in an expression context. When using it in the context of a for loop’s update clause, there relevant part is:

14.14.1.2. for语句的迭代

[…]

14.14.1.2. Iteration of for Statement

[…]

ForUpdate 部分,则按从左到右的顺序对表达式求值;它们的值(如果有的话)将被丢弃.如果任何表达式的求值由于某种原因而突然完成,则for语句由于相同的原因而突然完成;不会对突然完成的表达式右边的任何 ForUpdate 语句表达式求值.

→ First, if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If evaluation of any expression completes abruptly for some reason, the for statement completes abruptly for the same reason; any ForUpdate statement expressions to the right of the one that completed abruptly are not evaluated.

从字面上看,在代码上会有所不同,因为这些表达式产生不同的结果,然后将其丢弃.但是,这种差异在于无法观察到的行为,因此,通常将代码编译为一开始就不会产生值,因此不会有所不同.

Taking it literally there would be a difference in the code as these expressions produce different results which are then discarded. However, this difference lies in non-observable behavior and as a consequence, the code is usually compiled to not produce the value in the first place and hence does not differ.

答案是,代码可能存在差异,例如如果天真地进行编译,则可以保证观察到的行为是相同的.

So the answer is, the code may have differences, e.g. when compiled naively, however the observable behavior is guaranteed to be the same.

这篇关于Java中前缀和后缀++运算符之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆