• Post Reply Bookmark Topic Watch Topic
  • New Topic

Order of execution inside if statement  RSS feed

 
Nenad Cikic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Nenad Cikic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!