Yes.
Expression | Result | Evaluation Order |
---|---|---|
true && true | true | both operands evaluated |
false && true | false | only first operand evaluated |
true && false | false | both operands evaluated |
false && false | false | only first operand evaluated |
true & true | true | both operands evaluated |
false & true | false | both operands evaluated |
true & false | false | both operands evaluated |
false & false | false | both operands evaluated |
The & operator combines two boolean values using the rules for AND, but always evaluates both operands. This is useful when you want both operands to be evaluated no matter what values are returned.
The disadvantage is a possible loss of speed.
Most Java programs are written using the &&
operator whenever an AND is
needed.
The programmer must be careful of side-effects, though.
Usually this is done by writing methods that are "pure functions" as much
as possible.
A pure function is a method that returns a value and is free of
side effects.
Methods that are not pure functions should usually be kept out of boolean
expressions.
For example,
the final version of the method computeMaximum()
in the previous
example is a pure function.
It computed the maximum,
but did not itself save the result.