Roedy Green said:
Java is free to do the boxing with any
method it wants.
No it is not free.
It is fixed by the language specification that only those with the range
[-128 127] will result in a common Integer instance.
It never occurred they would "box" themselves in that way. I guess
the problem is people puzzled by == behaving strangely or
unpredictably.
They can still use any method to get that result, either direct
inlining or a method that produced one of those prebuilt Integers.
I guess then they constrained new Integer NOT to do that. Too bad
because 99% of the time that is what you want.
.
My explanation is quite lengthy and prone to criticism from "the other side
of the fence", so I put myself at risk when
a) I attempt it without sound premise.
b) I attempt it on an internet forum such as Usenet, where your target
audience is generally at the bottom end of intellect (and so here I am).
I'll give it a small shot anyway.
The problem is actually the result of the exposing of a constructor, which
implicitly exceeds requirements.
A client has never cared about "newness" of an object, only the contracts
that that object supports.
In the event that the object is mutable (such that an effect of the
invocation of an operation can be observed on some other operation), then
still "newness" has never mattered - merely, identity of the object
(contract implementation identity) such that operation O can be invoked on
'some supporting subject' (A), have an effect observed, while operation O
can also be invoked at some other time and have an effect observed on 'some
other supporting subject' (B), but not A. Since we distinguish A and B, then
this implies that they have identity - whether or not they are new, is never
relevant. This all assumes an OO paradigm (which has an awful contradictory
paradox, which I have omitted).
Since Integer is both immutable (cannot have a side effect observed after
invocation of an operation - or simply, all of its operations a monadic (see
Monads - Haskell)) and it exposes a constructor (exceeding requirements),
Sun have had to concede at some point that they believe will be optimal for
a general set of use cases. This atrocity is quite minor in the greater
scheme of all atrocities that are evident in the language.
ContractualJ prohibits the use of constructors: "All constructors are
declared private, and throw java.lang.UnsupportedOperationException, unless
they are implicit constructors of anonymous classes. Exposing construction
details (as Java constructor semantics implies) is not permitted." [
http://contractualj.com/ ] and attempts to demonstrate the optimal
workarounds (but still far from optimal once you exclude the premise of Java
itself) within the given context (that is, Java).
Although this explanation is far from complete, I am trying to complete a
publication that touches on this topic. The publication is a preliminary to
the release of my language specification (which is even more far from
complete).