Evaluation order of sub expressions in Java expressions

I have the following code snippets:

int x=2,y=3;
if ( (y == x++) | (x < ++y) )
// rest of code

I know that in C you are taught not to evaluate the order of Lai sub expressions, because it is not guaranteed to be any order at all Therefore, this code will make mistakes, and the Boolean value generated by the expression in the condition cannot be guaranteed to be true (for example, y can be incremented before it is calculated in the first equality test) Since I read this code in the Java certificate, I don't think this is the case with Java I mean, do I guarantee that the evaluation order in Java is always from left to right? So the above expression should always be true

Solution

Yes, it guarantees execution from left to right - or at least, just like it This is defined in JLS 15.7:

|The first operand operator of will evaluate 3 = = 2 and generate false, but the second operand will evaluate 3 < 4 and the benefit is true Having said that, I will certainly avoid code that needs to be read carefully

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>