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

2

112

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?

Mechanistic answered 10/4, 2015 at 8:48 Comment(15)
Why would you expect z to be increased also? This doesn't make any sense to me.Coquillage
what is the problem they said only 1 expression is evaluated y++ or z++ depending on the conditionBangkok
sounds like a poorly written book, ternary operators have not changed since the beginging of java, afaikMercurialism
@JiříKantor: I believe mbader meant that the behavior produced is as you would expect, not that you would expect z to be increased.Blynn
@NimChimpsky...but it's the "official" study guide!!Bergman
Reading most of the answers posted so far, people seem to misinterpret the question. It's not "Why aren't both expressions evaluated?", but rather "Why does this book seemingly imply that it used to behave differently?"Belda
Actually, I have seen "As of date/version X" used to mean "We checked that this is true on date/in version X but we are not saying anything about earlier versions." I'm guessing that may be the meaning here. (Though you'd think it would be easy enough to check earlier versions of Java.) Anyway, that's more of an English issue than a programming one.Dietetics
I am not a native English speaker, buy maybe they are referring to eventual later versions of Java, rather than earlier versions.Campeche
@David Z: right, but “as of version x” just implies “no statement about other versions”, be it earlier, newer or alternative branches.Poultry
@DavidZ: English issues are programming issues when they stop you from getting your work done. This poorly worded comment made the OP stop what he was doing and waste time discovering that NOTHING HAS CHANGED. Programming is communication, to the compiler/interpreter and to whoever comes along later to maintain your code. I can't count the times that I have been reading some code, and had to stop becuase of something strange that /might/ have had something to do with the problem, only to find out that it was just badly "phrased".Etiolate
@DavidZ I agree that's probably what was meant. Assuming that's the case, this might have been better worded as, "As of at least Java 7," but that makes them sound as lazy as they are.Definiendum
I find it grammatically correct, but a strange remark to do. It hasn't changed in any Java version (not even Java 8), and is not expected to change anytime soon. It works the same in C, C++, C#, JavaScript, and other languages I'm not familiar with. Specifying Java 7 is just distracting.Button
Indeed I'm not saying this is good phrasing, only justifying how it might have seemed valid to the author of the book.Dietetics
It might have been better for the author to just have a general disclaimer at the beginning of the book "Things new in Java 8 will be noted, but things new in Java 7 or earlier may not be; using any information in this book for versions of Java prior to 7 is done at your own risk."Burkhart
FYI, the ternary operator exists in over 20 popular languages, and some obscure ones. It goes back to the CPL language, C adopted that syntax, and Java inherited it from there. I'd expect it to work essentially the same in all these languages and all versions of them. en.wikipedia.org/wiki/%3F:Carson
C
96

I'm one of the authors of the book this came from. While I didn't write that particular sentence, I agree the intent was "this was tested on Java 7". I'll make a note to remove that if we write another edition.

To be clear, the ternary operator has behaved the same way in Java 8, 7, 6, etc. And I'd be quite surprised if it changed in the future.

Cormier answered 11/4, 2015 at 19:16 Comment(0)
B
120

From the 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.

Similar wording also appears in JLS editions going back to 1.0. The behavior didn't change in Java 7; the study guide is just poorly worded.

Blynn answered 10/4, 2015 at 8:54 Comment(4)
So the answer is "There is no difference as of Java 7 and before, concerning the ternary operator", right?Mechanistic
Seems legit. I wrote a memo to the authors - looking forward to their answerMechanistic
You could also probably find a URL that compares the code of the operator between versions. If you're paranoid/curious.Hole
The numbers of badly written (or just flat out wrong) questions in these Oracle certifications is astonishing every time again..Ceremonious
C
96

I'm one of the authors of the book this came from. While I didn't write that particular sentence, I agree the intent was "this was tested on Java 7". I'll make a note to remove that if we write another edition.

To be clear, the ternary operator has behaved the same way in Java 8, 7, 6, etc. And I'd be quite surprised if it changed in the future.

Cormier answered 11/4, 2015 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.