Forums Register Login

Order of execution inside if statement

+Pie Number of slices to send: Send
Hello all from Croatia.
I am developing a small android application and i have a basic doubt about the java language.
Is it correct to write if(ob.subObj!=null && obj.subObj.someflag) ?
Am I sure that the first block is always first checked.
In C++ I think this states. I have looked in java docs but did not have found any reference to this.

I know i can change the code to make it more "secure", it's just my curiosity.

Thanks
+Pie Number of slices to send: Send
 

Nenad Cikic wrote:
Am I sure that the first block is always first checked.



Yes. Expressions are evaluated left to right. The LH operand will always be evaluated before the RH operand.

Additionally if you use && or ||, then if the value of the whole expression can be determined after evaluating the LH operand, then the RH operand is not evaluated. So in the case of &&, if the LH operand is false, then the RH operand won't be evaluated. The expression is false no matter what. Likewise with the LH operand being true in the case of ||.

For & and |, however, the RH operand is always evaluated. There is no short-circuiting.
+Pie Number of slices to send: Send
 

Nenad Cikic wrote:In C++ I think this states. I have looked in java docs but did not have found any reference to this.


Probably because you didn't look in the right place.

Try here and here.

Winston
+Pie Number of slices to send: Send
And welcome to the Ranch
+Pie Number of slices to send: Send
Thanks
I've never won anything before. Not even a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4547 times.
Similar Threads
Blackberry vs other devices
can i prepare for SCJP exam without any programming experience?
EJB's are easy to build....are they?
Issues with IE and Mozilla Firefox
FilenameFilter help plz
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:20:36.