Re: Eklige Vergleiche von Referenzen mittels ==



"Malte Schirmacher" skribis:

Dieses Interval [-128,127] ist imho sehr willkürlich gewählt und man
darf es wohl als "Zufall" betrachten, wenn

(Integer)15 == (Integer)15

»true« ergibt.
Möglich wäre es allemal, dass sich in der nächsten Version das Interval
auf [-1000000,1000000] erweitert oder auf [-1,1] verkürzt, denn dieses
Caching findet nunmal nur im Code der Klasse Integer statt.

Hmm, ganz zufällig ist es nicht. Die LangSpec schreibt ein
Mindest-Intervall von [-128 .. 127] vor.

..--[ http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7
|
| [...]
| If the value p being boxed is true, false, a byte, a char in the range
| \u0000 to \u007f, or an int or short 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.

Da steht auch (im Kommentar), dass es Implementationen freigestellt
ist, mehr zu cachen:

| Less memory-limited implementations might, for example, cache all
| characters and shorts, as well as integers and longs in the range
| of -32K - +32K.
|
'-------


Paul
--
The Java Language Specification: http://java.sun.com/docs/books/jls/index.htmls
.