Can someone answer it please: Compare “short circuit” logical operators (AND)&& and (OR) || with logical (AND)& and (OR) | | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can someone answer it please: Compare “short circuit” logical operators (AND)&& and (OR) || with logical (AND)& and (OR) |

26th Nov 2018, 4:43 AM
Mystery
Mystery - avatar
7 Answers
+ 3
short circuit logical operators are smarter than normal logical operator i.e. the short-circuit operator will evaluate the second expression only if needed Let me explain you this with an example Let say a=10 and b=20 Now consider the Boolean expression (a>b)AND(b>a) . We know that for AND operation the result is true iff both it's operand is true In the above expression the result of (a>b) is false so the entire expression becomes false. In short circuit, the second operand is evaluated only if needed. As the result of the first operand is false there is no need to evaluate the second expression. Where as in normal logical & both the operand is evaluated irrespective of the first operand result
26th Nov 2018, 6:33 AM
Rishi Anand
Rishi Anand - avatar
+ 3
It's a operator overloading in Java & is used both for bitwise and Boolean evaluation
26th Nov 2018, 6:38 AM
Rishi Anand
Rishi Anand - avatar
+ 3
Yes
26th Nov 2018, 6:40 AM
Rishi Anand
Rishi Anand - avatar
+ 3
The && and || are logical (boolean) operators, they are used to evaluate the operands as true/false. The & and | are bitwise operators, they are used to process numbers (mostly integrals) by performing bitwise calculation on each bits. There are differences in behaviour between these operators depending on the inputs (expressions, operands to evaluate). Have a look at the following link for more thorough explanation regarding the differences. https://stackoverflow.com/questions/4014535/differences-in-boolean-operators-vs-and-vs
26th Nov 2018, 9:25 AM
Ipang
+ 1
ohk so does that mean that & work as bitwise as well as logical operator in java? Rishi Anand
26th Nov 2018, 6:39 AM
Mystery
Mystery - avatar
0
ohk i do get your point. thank u. but i m also confused that & is considered a bitwise operator. what do you have to say about that? Rishi Anand
26th Nov 2018, 6:36 AM
Mystery
Mystery - avatar
0
ohk thanks Rishi Anand
26th Nov 2018, 6:40 AM
Mystery
Mystery - avatar