Boolean logic in Java: &&, ||, ! vs &, | and ^
Java has two sets of operators on boolean: &&, || and ! short-circuit, while &, | and ^ evaluate both sides and are also the bitwise operators on integers. Knowing which set you are reading is most of the battle.
| Logic | Java | Note |
|---|---|---|
NOT A | !a | ~ is for integers only |
A AND B | a && b or a & b | & evaluates both sides |
A OR B | a || b or a | b | | evaluates both sides |
A XOR B | a ^ b or a != b | Both logical on boolean |
A -> B | !a || b | No implication operator |
A <-> B | a == b | Equality of two boolean values |
TRUE, FALSE | true, false | Not interchangeable with 1 and 0 |
!a, a && b and a || b are the usual logical operators and bind in that order. a & b, a | b and a ^ b on two boolean values are also logical, with ^ being exclusive or, but they always evaluate both operands: x != null & x.isEmpty() throws when x is null, while the && form does not. Precedence places & above ^ above |, and all three above && and ||, so a | b && c is (a | b) && c.
Java's boolean is not an integer: if (flag == 1) does not compile, and != between two Booleans is exclusive or while == is the biconditional. ~ applies only to integral types. There is no implication operator; !a || b is the translation, and a chain a -> b -> c must be bracketed by hand as !a || (!b || c).
Tangibool does not export Java. The practical bridge is the truth table: export it as CSV, turn each row into a parameterised test, and let the engine's independence pairs tell you which rows a MC/DC-style suite needs. The parser here rejects &, |, ^ and ~ on input so that a formula copied from Java is not silently read with the wrong operator.
The common slip
Reading a & b as a typo for a && b. On Booleans it is a valid, non-short-circuiting AND; in a condition that guards a method call on a possibly null object, the difference is an exception.
Questions
Is ^ XOR in Java?
Yes, on both boolean and integer operands. The parser of this reference still rejects ^, because in other tools it is a power or a bitwise operator, and offers XOR as the repair.
Does precedence match this reference?
For !, && and ||, yes. The non-short-circuit operators sit between them with their own order, and == and != bind tighter than &, so a & b == c is a & (b == c).
Related
See also
- Boolean logic in C: logical vs bitwise operators
- Boolean logic in JavaScript: &&, ||, !, truthiness and precedence
- Single & and | are bitwise in most languages
- ^ means XOR in C, Java and Python, not AND or power
- != on Booleans is XOR
- MC/DC independence pairs, worked example with the engine’s coverage report
- Deriving test cases from a truth table without testing every row
Languages
Generated from the Tangibool engine, semantics tangibool-semantics/1, engine 1.0.0, build . Every table, map, simplification and verdict on these pages was computed by the Tangibool engine when the site was built, and every claim was re-checked by the same exact checker the app uses. Limits are stated where they apply. © 2026 Foreranger LTD.