Why are there 2 boolean type operators in Java?

boolean-operations, java

Solution

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 `&&`

Problem

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?

Original source

Related problems