3

I got the following code from a Java book that didn't have explanation about the results. I ran it and got the results noted below inline, but I need help understanding the results:

1. Integer i = -10;
2. Integer j = -10;
3. System.out.print(i==j);  //  ==> true
4. System.out.print(i.equals(j));   //  ==> true
5. Integer n = 128;
6. Integer m = 128;
7. System.out.print(n==m);  //  ==> false
8. System.out.print(n.equals(m));   //  ==> true

My questions are:

  • why does line #3 resolve to true? are they not two separate objects?
  • if line #3 is true, why does line #7 not true?

Thanks if advance.

3
  • 4
    Hint, integers in a specific range are cached ;) Have a look into the Integer class. Dec 2, 2012 at 19:07
  • @ThomasJungblut Thats the reason why should prefer Integer.valueOf(int) to new Integer(int)
    – AlexWien
    Dec 2, 2012 at 19:26
  • 1
    But here's an interesting question: Is it possible for #7 to ever be true? The answer is YES, since the upper limit (but not lower limit) of the Integer cache is configurable.
    – Hot Licks
    Dec 2, 2012 at 19:31

4 Answers 4

9

See Integer.valueOf(int i) source code.

It caches (use integer pool) the Integers between -128 and 127 (those are the default values, which can be customized via java.lang.Integer.IntegerCache.high)

0
2

To emphasize the level of absurdity resulting from using == on Integers, consider this line:

Integer a = 200, b = 200;
System.out.println(a < b || a == b || a > b);

Apparently, this should print true regardless of values because it looks like a tautology. It prints false, of course. The following, apparently a tautology of the same kind, prints the expected true value:

System.out.println(a <= b || a > b);
1
  • great one! I really liked it :)
    – dantuch
    Dec 2, 2012 at 20:02
0

Integer is an object, whereas int is a "scalar". Something like Integer i = 270;, under the covers, becomes Integer i = Integer.valueOf(270);. So when you create two Integer objects they are distinct (as demonstrated with ==).

However, the JDK, in valueOf, caches the creation of Integer objects with a small value -- -128 to 127, and previous versions of those are reused if available. So two apparently distinct instances of small Integers may actually be the same, although they appear to have been instantiated separately.

If you tried the same thing with int values the == compares would always be true, and the equals() calls would generate compile errors (because int isn't an object).

0

On question 1

According to the doc, a Integer class is wrapped around a primitive value, which simply means that you can expect the object to behave as a primitive when required (the comparison performed in Step. 3), and equally as an object when required (Step 4; this step isn't possible if int instead of an Integer class was used.).
If you swap the types in Steps 1 and 2 to String instead of an Integer, and perform the tests again, you'll obtained the desired results you were hoping to get.

On question 2

It has already been answered above: Integers in specific ranges (-128 and 127) are cached.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.