| Author |
Order of execution inside if statement
|
Nenad Cikic
Greenhorn
Joined: Nov 04, 2012
Posts: 2
|
|
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
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5888
|
|
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.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4756
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
And welcome to the Ranch
|
 |
Nenad Cikic
Greenhorn
Joined: Nov 04, 2012
Posts: 2
|
|
|
Thanks
|
 |
 |
|
|
subject: Order of execution inside if statement
|
|
|