wood burning stoves*
The moose likes Beginning Java and the fly likes Boolean operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean operators" Watch "Boolean operators" New topic
Author

Boolean operators

vigneswar rao
Greenhorn

Joined: Feb 12, 2006
Posts: 14
Hai ,

I have a small doubt on Boolean Operators can any explain with example.


What is the difference between the Boolean & operator and the && operator?


thank u
vigneswar!
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Dear vigne,

"&" this is a bitwise AND Operator and it is used normally between int and int parameters.

"&&" This is logically AND Operator which is defined between two boolean Operators.
I just show you an example:



Output:0
Explanation: in Binary 10 is represented by 01010 and 20 is represented by 10100 now when you apply AND operation between them you will get 0.


Now we proceed to && Operator



Output: False.
Explaination: Because in Logical AND Operation if any single field is false then the result will be false.

I think that this example is enough for your understanding.

If any concern then revert me back.





Cheer Up Vigne


The Best way to predict your future is to create it - Every great individual common man
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
But can't the & operator also be applied to boolean operands?


There is no emoticon for what I am feeling!
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5



Now, in the case of && (short circuit operation) if x is null, the statement will terminate after the first expression *BUT* if & was used in its place, this would throw a null pointer exception anytime x was null since it would always execute both sides.

There are other cases (I've leave that to the reader think about) where you always want both sides to be executed regardless of whether they both return true/false, although I'd tend to suspect such code to be somewhat unclear from the normal coding techniques of java.
[ March 21, 2006: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
... thanks for the post. Conditional evaluation distinguishes && from & on booleans. Although your example could be rewritten:

but others could not:
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
As was stated, if & is used as a boolean operator it always evaluates both sides, which is slower and can cause errors to be thrown. There really isn't a reason to need to use it like that.

if(str.length()<5 & obj != null)

if the left side is false there is no need to look at the other side, but it still will do it.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
vigneswar rao
Greenhorn

Joined: Feb 12, 2006
Posts: 14
thanks to all
clarifying my doubt
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Originally posted by Rusty Shackleford:
There really isn't a reason to need to use it like that.


That's not completely true, if the code you were writing invoked methods that performed some kind of recovery in the event one side was false, you *might* want both sides to be executed. This amounts to side effect methods that as I said in my original post, is not commonly practicied in java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean operators