1

I am working with Java code in JSP and I am trying to compare strings and I am having problem with that.

I have declared two strings

s1 = "din";
s2 = "din";

However, the if (s1 == s2) never executes. Can someone help me?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
pa12
  • 1,493
  • 4
  • 19
  • 40
  • 3
    Please show your actual code. Are you using scriptlets? (please don't) – Matt Ball Mar 12 '11 at 23:31
  • 3
    On some JVMs this will actually work because they intern all string constants. – Neil Mar 12 '11 at 23:44
  • 1
    You should use .equals to compare equality (as others have mentioned). However, note that in the example you give (s1 == s2) will often equal true, as the string literals will be interned (s1 and s2 will refer to the same object) – Neil Mar 12 '11 at 23:44
  • 1
    what do you mean: "never executes"? – lukastymo Mar 12 '11 at 23:49
  • See also http://stackoverflow.com/questions/995918/java-string-comparison, http://stackoverflow.com/questions/1530864/java-why-doesnt-my-string-comparison-work, http://stackoverflow.com/questions/1896145/basic-java-question-string-equality, all from the links on this page. – Gabe Mar 13 '11 at 00:24
  • 2
    @Neil: not on "some JVMs", but on all, since the language specification prescribes this. (I think in dins code there are not actually string literals.) – Paŭlo Ebermann Mar 13 '11 at 01:31
  • This question is why schools still need to teach pointers. – Scott Mar 13 '11 at 02:44

4 Answers4

12

The operator == for Strings compares for reference equality, not value equality.

Try calling equals instead of using ==:

if (s1.equals(s2)) { ... }
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
6

When comparing strings you should always use the equals method, not ==:

if(str1.equals(str2))

...would be the correct way to do things.

Where confusion arises is cases like the following:

String str1 = "hello";
String str2 = "hello";
System.out.println(str1==str2);

The above will actually print out true. However, save for academic purposes like the above, you shouldn't ever use it. If you're using == you're checking if the values are physically the same object on the heap. If you're using .equals() you're checking that they're meaningfully equal, even if they're actually two separate objects. Sometimes, especially where literals are involved such as above (or when you manually call the intern() method) Java will automatically make two separate string objects point to the same object for performance reasons. However, there's no logical guarantee this will happen (unless you want to bother yourself with explicit details of the JLS, and even then it's only guaranteed sometimes) and most of the time it won't. Take the following for example:

String str1 = "hello";
String str2 = "he";
str2 += "llo";
System.out.println(str1 == str2);

Now it prints false, because despite being meaningfully equal we haven't hit the same optimisation that Java was providing previously.

In both cases above, using ,equals() would return true.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • There are some guarantees about when strings are the same object in the Java Language Specification, but you are right, better use always `equals`. – Paŭlo Ebermann Mar 13 '11 at 01:33
  • @Paŭlo Ebermann good point - I meant more from a logical point of view rather than knowing JLS details. – Michael Berry Mar 13 '11 at 01:34
5

You use 'equals' when you compare strings.

if(s1.equals(s2)) { true }

heldt
  • 4,166
  • 7
  • 39
  • 67
2

What are you trying to achieve?

Compare String's or their values? If you are expecting that your (if) condition should return true then use

s1.equals(s2);
SSG
  • 192
  • 2
  • 12