Reading the JAVA 13 SE specification, I found in chapter 5, section 5.1.7. Boxing Conversion the following guarantee:
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
I find it odd that values of type byte are left out from that wording.
For example, in a code such as:
Byte b1=(byte)4;
Byte b2=(byte)4;
System.out.println(b1==b2);
We have a constant expression of type byte, and after the boxing, the values of b1 and b2 may or may not be the same object.
It works actually the same way without the cast:
Byte b1=4;
Here, we have a constant expression of type int in an assignment context. So, according to the spec
A narrowing primitive conversion followed by a boxing conversion may be used if the variable is of type Byte, Short, or Character, and the value of the constant expression is representable in the type byte, short, or char respectively.
So the expression will be converted to byte, and that byte type value will be boxed, so there is no guarantee that the value is interned.
My question is am I right in interpreting the spec, or am I missing something? I have looked if the spec requires using of method Byte.valueOf() for the boxing (for which it would be guaranteed), but it does not.