Is caching of boxed Byte objects not required by Java 13 SE spec?
You understand it correctly. The end of the same 5.1.7 section (from https://docs.oracle.com/javase/specs/jls/se13/html/jls-5.html) says:
A boxing conversion may result in an OutOfMemoryError if a new instance of one of the wrapper classes (Boolean, Byte, Character, Short, Integer, Long, Float, or Double) needs to be allocated and insufficient storage is available.
Byte
would not be there if it was expected to be pre-generated.
Another thing, still from the same paragraph:
Ideally, boxing a primitive value would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rule above is a pragmatic compromise, requiring that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly. For other values, the rule disallows any assumptions about the identity of the boxed values on the programmer's part. This allows (but does not require) sharing of some or all of these references.
Not a "proof", but perhaps it is worth to mention:
Integer
describes the boxing promise, 13 and even 7
* Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS.
The text is the same, even though the implementation has changed over time.
Byte
has no such statement, though it is cached too. 7, 13. The cache is there in both, but there is not a single word about it (and neither about boxing).
TL;DR this has been fixed with JDK 14, which now includes byte
.
I consider this a specification bug, result of multiple rewritings.
Note the text of the JLS 6 counterpart:
If the value p being boxed is
true
,false
, abyte
, achar
in the range \u0000 to \u007f, or anint
orshort
number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
Here, byte
is explicitly mentioned as being boxed to an object with canonical identity, unconditionally. Since all bytes are in the -127..128 range, there was no need for adding such a restriction.
But note that long
has not been mentioned.
Then, meet JDK-7190924, 5.1.7: JLS does not mention caching of autoboxed longs
In the comments, you can see, how it happened.
In his first comment, Alex Buckley criticizes that "byte is a type, not a value", not considering that "byte" could mean "all values in the byte range", but since he also assumes that "number" originally meant "literal" (instead of, e.g. "numeric value"), he focuses on the point that all integer literals are either int or long.
His first draft uses the term "integer literal" and removes the types completely. A slightly modified version of it made it into the Java 8 JLS:
If the value
p
being boxed is an integer literal of typeint
between-128
and127
inclusive (§3.10.1), or the boolean literaltrue
orfalse
(§3.10.3), or a character literal between'\u0000'
and'\u007f'
inclusive (§3.10.4), then leta
andb
be the results of any two boxing conversions ofp
. It is always the case thata == b
.
So in Java 8, the type doesn't matter at all, but the guaranty is limited to literals.
So this would imply that
Byte b1 = 4;
does evaluate to a canonical object due to the integer literal, where as
Byte b1 = (byte)4;
may not, as (byte)4
is a constant expression but not a literal.
In his next comment, years later, he considers "constant expressions", which can indeed be typed, and reformulates the phrase, bringing back the types, "boolean, char, short, int, or long", having added long, but forgotten about "byte".
This resulting phrase is what you've cited, which is in the specification since Java 9.
The omission of byte
surely isn't intentional, as there is no plausible reason to omit it, especially, when it was there before, so this would be a breaking change when taken literally.
Though, restricting the caching to compile-time constants, when JLS 6 specified it for all values in the range without such a restriction, is already a breaking change (which doesn't matter in practice, as long as it is implemented via valueOf
, which has no way of knowing whether the value originated from a compile-time constant or not).
As a side note, the documentation of Byte.valueOf(byte)
explicitly says:
...all byte values are cached
as long as since Java 7.