Preparing for the Oracle Certified Associate Java SE 8 Programmer 1 exam, I came across the following paragraph about the ternary expression in the official Study Guide:
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: [...]
It says that only one of the two expressions is evaluated, demonstrating with the following example:
int y = 1;
int z = 1;
int a = y < 10 ? y++ : z++;
Here, only y
increments, but z
does not, as you would expect.
What I am stumbling across is the beginning of the paragraph (marked in yellow) where it says "As of Java 7, ...". I tested the same code with Java 1.6 and I can't find a difference in the behavior. I expected Java 1.6 to evaluate both expressions just from the information given in the paragraph. Does anyone have an idea what they wanted to say with "As of Java 7, ..."?
Edit: To avoid confusion: It boils down to the question, Since they write 'As of Java 7', was there anything that changed concerning the ternary operator, when switching from Java 6 to Java 7?
y++
orz++
depending on the condition – Bangkok