Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there 2 boolean type operators in Java?

As said in thinking in Java if you have 2 boolean objects, x and y you can use either x= x&&y and x=x&y on them, so why its necessary having both types?

like image 981
Eduardo EPF Avatar asked Dec 02 '22 20:12

Eduardo EPF


1 Answers

The two operators act differently:

  • & will always evaluate both operands
  • && is short-circuiting - if the first operand evaluates to false, then the overall result will always be false, and the second operand isn't evaluated

See the Java Language Specification for more details:

  • Section 15.22 (Boolean Logical Operators) for &
  • Section 15.23 (Conditional-And Operator) for &&
like image 86
Jon Skeet Avatar answered Dec 04 '22 11:12

Jon Skeet