0

I have a question about comparing String and Integer objects...

a. Comparing String references:

    String string1 = "Hi";
    System.out.printf("%s \n", string1);
    String originalString = string1;
    System.out.printf("%-9s %-9s  %b \n", string1, originalString, originalString == string1);
    System.out.println();

    string1 += " there";
    originalString += " there";
    System.out.printf("%-9s %-9s  %b \n", string1, originalString, originalString.equals(string1));
    System.out.printf("%-9s %-9s  %b \n", string1, originalString, originalString == string1);

Produced output:

    Hi 
    Hi        Hi         true 

    Hi there  Hi there   true 
    Hi there  Hi there   false 

Here, the last line compares the addresses and as to be expected gives false. OK so far ;)

b. Comparing Integer references:

    Integer integer1 = 10;
    System.out.printf("%d \n", integer1);
    Integer originalInteger = integer1;
    System.out.printf("%d %d  %b \n", integer1, originalInteger, originalInteger == integer1);
    System.out.println();

    integer1++;
    originalInteger++;
    System.out.printf("%d %d  %b \n", integer1, originalInteger, originalInteger == integer1);

Produced output:

    10 
    10 10  true 

    11 11  true 

By the time the last line is printed out, both 'integer1' and 'originalInteger' are referencing completely different objects... nevertheless

   originalInteger == integer1   --> true  ???

Does this imply that not the addresses of the objects but the the contents of the objects are compared? In other words, is it because, with type wrappers, the values are always 'unboxed' before being compared?

So, to resume:

originalString == string1     --> false
originalInteger == integer1     --> true

I don't understand why originalInteger == integer1 --> true

Thank you

Chris
  • 117
  • 1
  • 5
  • Previous questions about this include http://stackoverflow.com/q/3130311/978917, http://stackoverflow.com/q/1700081/978917, http://stackoverflow.com/q/15024933/978917, and many others. – ruakh Jun 20 '15 at 07:31

1 Answers1

0

The == compares the reference of an object (the "address", as you put it), as you understood (correctly) for the String example.

When you apply the ++ operator to an Integer, you are in fact outboxing it to a primitive int, incrementing it, and autoboxing it back to an Integer, so theoretically, you could get a different object there. However, the Integer class plays a "dirty" trick here (read: neat optimization). For values up to 127 (inclusive), Java keeps a cache of Integers and returns the same object whenever this value is autoboxed. So, in fact, you would be getting the same object, so originalInteger == integer1 would return true. If you repeat this example with a larger integer, let's say 500, you'd get false like you expect.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Re: "If you repeat this example with a larger integer, let's say `500`, you'd get `false` like you expect": This is not guaranteed by the spec. The spec says that ideally, value-equality would imply reference-equality for all autoboxed integers, but that this isn't feasible in all implementations, so it only requires this for integers in the range [-128,127). For integers outside that range, it neither requires nor forbids it. – ruakh Jun 20 '15 at 07:35