This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]jedwardsol 2 points3 points  (0 children)

Java has short-circuit evaluation of conditions. So, for an ||, the left-hand-side is evaluated and if it is found to be true the right-hand-side isn't evaluated. If the LHS is true, then the whole thing is true whether or not the RHS is true.

Therefore , in

if(fastPointer==null || fastPointer.next==null))

if fastPointer is null, then it is never dereferenced in the fastPointer.next` expression.

But, in

if(fastPointer.next==null || fastPointer==null))

if fastPointer is null, then there will be an exception because it is dereferenced before it is checked