Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boolean operators

 
vigneswar rao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But can't the & operator also be applied to boolean operands?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... thanks for the post. Conditional evaluation distinguishes && from & on booleans. Although your example could be rewritten:

but others could not:
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vigneswar rao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all
clarifying my doubt
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic