Java 中的三元运算符仅计算一个表达式,因为 Java 7 - Java 1.6 和更低版本中有什么不同吗?

Ternary operator in Java only evaluating one expression since Java 7 - was that different in Java 1.6 and lower?

准备Oracle Certified Associate Java SE 8 Programmer 1考试,我在官方学习指南中看到了以下关于三元表达式的段落:

Ternary Expression Evaluation
As of Java 7, only one of the right-hand expressions of the ternary operator will be evaluated at runtime. In a manner similar to the short-circuit operators, if one of the two right-hand expressions in a ternary operator performs a side effect, then it may not be applied at runtime. Let's illustrate this principle with the following example: [...]

它表示只对两个表达式中的一个求值,用下面的例子演示:

int y = 1;
int z = 1;
int a = y < 10 ? y++ : z++;

这里,只有 y 递增,但 z 不会,如您所料。

我偶然发现的是段落的开头(标记为黄色),上面写着 "As of Java 7, ..."。我用 Java 1.6 测试了相同的代码,但我找不到行为上的差异。我希望 Java 1.6 仅根据段落中给出的信息来评估这两个表达式。有没有人知道他们想用 "As of Java 7, ..." 表达什么?

编辑: 为避免混淆:归结为一个问题,自从他们写了 'As of Java 7',当从Java 6 到 Java 7?

来自Java 6 JLS

At run time, the first operand expression of the conditional expression is evaluated first; if necessary, unboxing conversion is performed on the result; the resulting boolean value is then used to choose either the second or the third operand expression:

  • If the value of the first operand is true, then the second operand expression is chosen.
  • If the value of the first operand is false, then the third operand expression is chosen.

The chosen operand expression is then evaluated and the resulting value is converted to the type of the conditional expression as determined by the rules stated above. This conversion may include boxing (§5.1.7) or unboxing conversion. The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.

类似的措辞也出现在可追溯到 1.0 的 JLS 版本中。 Java 7 中的行为没有改变;学习指南措辞不佳。

我是这本书的作者之一。虽然我没有写那个特定的句子,但我同意其意图是 "this was tested on Java 7"。如果我们再写一个版本,我会记下来删除它。

明确地说,三元运算符在 Java 8、7、6 等中的行为方式相同。如果将来发生变化,我会感到非常惊讶。