Originally posted by Namaste Sathi: hi, what are the particular situatios that short-circuit logical operator (|| and &&) is used against other logical operators(| and &)? please help me with it. thanks.
The short circuit operators can save you from things like NullPointerExceptions and can cause a statement to only be evaluated if both operands are tested. For example:
That will save you from a runtime exception because if obj is null then obj.hashCode() will not be called which would result in a NullPointerException being thrown. Also consider this:
If the result of a + b is 6 then the statement c = c + 3 will not be evaluated so in that case c would not change.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher