Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Short-Circuit Logical Operator!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Short-Circuit Logical Operator!!!" Watch "Short-Circuit Logical Operator!!!" New topic
Author

Short-Circuit Logical Operator!!!

Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
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
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://aspose.com/file-tools
 
subject: Short-Circuit Logical Operator!!!
 
Similar Threads
| operator
Bitwise operator " | "
| and || operators
|| operand
Q on Operators in Dan Chisholm's site