-4

Codes :

Integer a1 = 100;
Integer a2 = 100;
System.out.println(a1 == a2);   // true

Integer b1 = new Integer(100);
Integer b2 = new Integer(100);
System.out.println(b1 == b2);   // false

Integer c1 = 150;
Integer c2 = 150;
System.out.println(c1 == c2);   // false  

Java designs that when using AutoBoxing, values between -128 and 127 appear to refer to the same Integer objects, which causes different results of the first code fragments and the last one

My question is : Why does java design it like this, are there any advantages ?

Bin
  • 980
  • 2
  • 12
  • 24
  • What advantages do you think it might have? What did your research on the topic suggest? Try running the same program with `-XX:+AggressiveOpts` http://stackoverflow.com/a/11882284/57695 – Peter Lawrey Nov 16 '12 at 13:24
  • 1
    Try creating an array of a million small Integers, you'll see the advantage. – Marko Topolnik Nov 16 '12 at 13:24
  • @dystroy I'm not sure it is a duplicate in the sense that this question asks for the advantages of caching integer rather than why the code behaves the way it does. – assylias Nov 16 '12 at 13:25

3 Answers3

3

The simple rationale is that it's useful/efficient to have a set of Integers already created and available for boxing. It's very likely that if an application will need boxed integers, they're going to be in a certain range (e.g. 1... whatever). The fact that they're boxed up to -127/128 is a simple design heuristic based upon what's likely to provide benefit without pre-boxing a huge number of integers.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • +1 - I imagine that the Java engineers, tested a number of programs that were typical of customer applications, and determined that caching 256 integers was empirically a good size for the cache. – Stephen C Nov 16 '12 at 13:50
2

It is a sort of optimization as a lot of the numerical operations are using int values in the mentioned interval.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
2

Integers are cached for values between -128 and 127. This is part of the Java Language Specification:

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 reason for that behaviour is given in the same section: the default behaviour would be for == comparisons to always return true but it has been limited to that range for performance reasons.

Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. [...]
This [range limitation] 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.

assylias
  • 321,522
  • 82
  • 660
  • 783