2

Here they are the same instance:

Integer integer1 = 127;
Integer integer2 = 127;
System.out.println(integer1 == integer2);  // outputs "true"

But here they are different instances:

Integer integer1 = 128;
Integer integer2 = 128;
System.out.println(integer1 == integer2);  // outputs "false"

Why do the wrapper class case the objects in this range? I have read the JLS 5.1.7 Boxing Conversion. My question is why architect decided to do maintain case for this range ?

Hitesh
  • 3,449
  • 8
  • 39
  • 57
niiraj874u
  • 2,180
  • 1
  • 12
  • 19

2 Answers2

1

Have a look to this answer https://stackoverflow.com/a/20948389/1897572

Just wondering, why between -128 and 127?

A larger range of integers may be cached, but at least those between -128 and 127 must be cached because it is mandated by the Java Language Specification (emphasis mine):

If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and

127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

The rationale for this requirement is explained in the same paragraph:

Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using

existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. [...]

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 char and short values, as well as int and long values in the range of -32K to +32K.

How can I cache other values outside of this range.?

You can use the -XX:AutoBoxCacheMax JVM option, which is not really documented in the list of available Hotspot JVM Options. However it is mentioned in the comments inside the Integer class around line 590:

The size of the cache may be controlled by the -XX:AutoBoxCacheMax=<size> option.

Note that this is implementation specific and may or may not be available on other JVMs.

Community
  • 1
  • 1
OpenStark
  • 476
  • 2
  • 8
  • is it like many algorithms use small integers in their calculations, so avoiding the object-creation overhead for these values tends to be worthwhile? – niiraj874u May 15 '14 at 08:44
  • I thanks for your help.. but, really I can't get this.. please elaborate me in Lehman Language – niiraj874u May 15 '14 at 09:07
0

I searched for this and found from other source and @nos, that it is like many algorithms use small integers in their calculations, so avoiding the object-creation overhead for these values tends to be worthwhile.

PS : if any one wants to add more reason behind this casing idea, You can edit my answer. this will later help people like me.

niiraj874u
  • 2,180
  • 1
  • 12
  • 19