-1

If I have:

Boolean isEqual = compare("Hello World");

Boolean compare(String c) {
    String s = "Hello World";
    return s == c;
}

In my experience, this returns true. Why?

solarbabies
  • 302
  • 1
  • 12

1 Answers1

0

because its a string literal that has been interned, the same instance is used.

Use new String("Hello World") to make it false.

Also asked many many many times before on here

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 3
    so why do you answer this and not reffering to the original answer? – aviad Dec 14 '14 at 20:07
  • Cool, thanks! Sorry for the duplicate, I'll delete this one. I'm on mobile so I was having a hard time finding someone else who asked this question. – solarbabies Dec 14 '14 at 20:10