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.
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
posted
0
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.