Thanks in advance.
[ January 23, 2006: Message edited by: Gabriel White ]
Originally posted by Ernest Friedman-Hill:
In Java 5, you can do that assignment, actually, via "autoboxing".: The compiler will automatically change it into something like this:
a[i] = new Integer(num);
Of course, in earlier Java versions, you can do this manually.
As a note, a "new" instance of Integer is not guaranteed when boxed, unless 'num' is outside some specific bounds: -128 to 127.
Related fun: http://jqa.tmorris.net/GetQAndA.action?qids=7
[Tony]: As a note, a "new" instance of Integer is not guaranteed when boxed, unless 'num' is outside some specific bounds: -128 to 127.
Rather, there is no guarantee either way, outside those bounds. Within the bounds, it's guaranteed that you'll get the same result for any two boxing conversions of the same number. So
(Integer) 1 == (Integer) 1
is always true, but
(Integer) 256 == (Integer) 256
may or may not be true. On Sun's current JDK 5, it's false (as Tony already knows). For those who are curious, the code responsible for this can be seen in Integer.valueOf():
See JLS3 5.1.7, in particular:
For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.
This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all characters and shorts, as well as integers and longs in the range of -32K - +32K.
By the way Tony, I haven't forgotten the previous thread about switch statements. I was busy for a bit, then you were gone, but now that we're both here again I'll get back to that shortly. Sorry for the delay. I'm sure you've been on the edge of your seat waiting.
[ January 23, 2006: Message edited by: Jim Yingst ]