Autoobject implemented using valueOf() ... in OpenJDK. If this is your implementation, read ... if not, skip below.
((Boolean)true) == Boolean.TRUE ((Boolean)true) == Boolean.valueOf(true)
The Java documentation states that Boolean.valueOf() always returns Boolean.TRUE or Boolean.FALSE , so your link comparisons will succeed in these cases.
((Integer)1) == Integer.valueOf(1)
In this particular example, as part of the OpenJDK implementation with default settings, this is likely to work because you selected the value <128, which is cached at startup (although this can be overridden as the arg command line). It can also work for large values if it is often used enough for caching. If you are not working on “safe” assumptions about the Integer cache, do not expect reference comparisons to be equality.
Long , Short , Character and Byte way also implement this caching, but unlike Integer it is not configurable. Byte will always work if you are comparing autobox / valueOf() links, since obviously you cannot go beyond the range. Float and Double not surprisingly always creating a new instance.
Now, in purely general terms? See this JLS section - you MUST are assigned equal references for boolean and any int or char in the range from -128 to 127. There is no guarantee for anything else.
Olipro Aug 07 '15 at 7:00 2015-08-07 07:00
source share