File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Short-Circuit Logical Operator!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Short-Circuit Logical Operator!!!
 
Similar Threads
Q on Operators in Dan Chisholm's site
Bitwise operator " | "
| operator
|| operand
| and || operators