aspose file tools
The moose likes Beginning Java and the fly likes Order of execution inside if statement Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Order of execution inside if statement" Watch "Order of execution inside if statement" New topic
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
    
    6

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
    
    7

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
    
    4
And welcome to the Ranch
Nenad Cikic
Greenhorn

Joined: Nov 04, 2012
Posts: 2
Thanks
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Order of execution inside if statement
 
Similar Threads
can i prepare for SCJP exam without any programming experience?
Issues with IE and Mozilla Firefox
FilenameFilter help plz
Blackberry vs other devices
EJB's are easy to build....are they?