If i have the following if statement
if ( (row != -1) && (array[row][col] != 10) ) {
....
}
Where row is an int value and array is an int[][] object.
My question is, if this will throw an exception if row = -1 as the array won’t have a -1 field, so out of bounds exception? Or will it stop at the first part of the if, the (row!=-1) and because that is false, it will ignore the rest?
Or to be sure it doesn’t throw exception, i should separate the above if statement into two?
(Pls, don’t tell me to check this out for my self 🙂 I’m asking here ’cause i wanna ask a followup question as well …)
It will stop safely before throwing an exception
The
&&is a short-circuiting boolean operator, which means that it will stop execution of the expression as soon as one part returns false (since this means that the entire expression must be false).Note that it also guaranteed to evaluate the parts of the expression in order, so it is safe to use in situations such as these.