For Java 111, JLS 5.1.7 states this:
At run time, boxing conversion proceeds as follows:
- If p is a value of type boolean, then boxing conversion converts p into a reference r of class and type Boolean, such that r.booleanValue() == p
[...]
If the value p being boxed is the result of evaluating a constant expression (§15.28) of type boolean, char, short, int, or long, and the result is true, false, a character in the range '\u0000' to '\u007f' inclusive, or an integer in the range -128 to 127 inclusive, then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.
The JLS does not explicitly state that the <wrapperType>::valueOf
method is used for autoboxing. In practice, a Java compiler typically does implement it that way, but it is an implementation detail, and could (in theory) change.
So ... apart from the "constant expression" case ... you should NOT rely on autoboxed boolean values being identical to the Boolean.TRUE
or Boolean.FALSE
object references2.
1 - Earlier versions of the JLS made a stronger statement about boxing. But this is clearly a deliberate change to the spec.
2 - Your application could take steps to manually box boolean values using Boolean::valueOf
(or some other way). But that's a different scenario. And probably not recommended, since you have to ensure that you do this consistently.