• Post Reply Bookmark Topic Watch Topic
  • New Topic

Are conditional statements processed in order?  RSS feed

 
R Hamrick
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words

Can you assume that condition_1 is always checked first followed by 2 and then 3 or could it be random? I had always assumed it was in order but recently encountered a problem where the second condition was tossing an error that the first condition should have checked. In my case it something along the lines of this

I had assumed that the check for null would execute first and if it was null the checks would stop and there would never be an attempt to call the method resulting in a nullPointerException.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R Hamrick wrote:In my case it something along the lines of this

I had assumed that the check for null would execute first and if it was null the checks would stop and there would never be an attempt to call the method resulting in a nullPointerException.


Correct. If the "object" instance variable is equal to null, then the left operand to the logical AND operator would be false, and the right operand would be short circuited.

Henry
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is bound to be something in the Java® Language Specification (=JLS) about it. It should say that expressions are always evaluated left to right. Yes, here it is. Beware: the JLS can be difficult to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!