0

So I understand that the == checks for equality in the reference number (the address of objects in memory). And the .equals() checks for the content of the objects.

String s = "test";
String s2 = "test";

I'm creating two different string objects but yet, I get the following:

s == s2;  //true, I dont know why, aren't s and s2 two different objects with 
            different internal values
s.equals(s2); //true, which I understand
PTheCoolGuy
  • 63
  • 1
  • 7
  • 3
    this has been explained in detail in [this thread](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – eis Dec 07 '14 at 18:10

1 Answers1

5

aren't s and s2 two different objects

No. s and s2 refer to the same Object that has been interned in the String pool

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • so whenever you say `String something = "something"` the reference variable `something` is referring to the same object? – PTheCoolGuy Dec 07 '14 at 18:16
  • Yes. provided that `"something"` has already been interned. You could use `new String("test")` to explicitly create a different object to return `false` for the 1st test – Reimeus Dec 07 '14 at 18:27