This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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