7

Using the standard loop, I compared a substring to a string value like this:

if (str.substring(i, i+3) == "dog" ) dogcount++;

and it broke the iteration. After the first increment, no further instances of "dog" would be detected.

So I used substring.equals, and that worked:

if (str.substring(i, i+3).equals("dog")) dogcount++;

My question is, why? Just seeking better understand, thx.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Jacob Stevens
  • 854
  • 1
  • 8
  • 17
  • 3
    this has been asked/answered so many times http://stackoverflow.com/questions/767372/java-string-equals-versus – Nikita Ignatov May 25 '12 at 18:16
  • 2
    disagree with downvoters. @Cshift3iLike this is such a basic question that it's not surprising that one would have hard time finding the answer. – MK. May 25 '12 at 18:19
  • 3
    It seems some of the members are having fun with the people who are answering. Why all the unnecessary downvotes?! It's stupid! – Kazekage Gaara May 25 '12 at 18:26

5 Answers5

10

You should use equals() instead of == to compare two strings.

s1 == s2 returns true if both strings point to the same object in memory. This is a common beginners mistake and is usually not what you want.

s1.equals(s2) returns true if both strings are physically equal (i.e. they contain the same characters).

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Thx much. But why does using == break the iteration? Even if the count variable isn't incremented, I'd expect the iteration to continue. – Jacob Stevens May 25 '12 at 21:49
  • You never mentioned what the "loop" was in your question, so I can't be sure. The loop could be `while(true) { }` for all I know. – Alex Lockwood May 25 '12 at 21:54
  • It's from http://codingbat.com/prob/p111624: public boolean catDog(String str) { int dogcount = 0; int catcount = 0; for (int i=0; i – Jacob Stevens May 25 '12 at 22:09
8

== compares references. You want to compare values using equals() instead.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Oleksi
  • 12,947
  • 4
  • 56
  • 80
4

== compares references(storage location of strings) of strings

and

.equals() compares value of strings

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
4

For String comparison, always use equals() method. Because comparing on == compares the references.

Here's the link for further knowledge.

You might also want to read this for understanding difference between == and equals() method in a better way along with code examples.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
3

"String" is an object in Java, so "==" compares the references, as stated. However, code like

String str1 = "dog";
String str2 = "dog";
if(str1==str2)
    System.out.println("Equal!");

will actually print out "Equal!", which might get you confused. The reason is that JVM optimizes your code a little bit when you assign literals directly to String objects, so that str1 and str2 actually reference the same object, which is stored in the internal pool inside JVM. On the other hand, code

String str1 = new String("dog");
String str2 = new String("dog");
if(str1==str2)
    System.out.println("Equal!");

will print out nothing, because you explicitly stated that you want two new objects.
If you want to avoid complications and unexpected errors, simply never use "==" with strings.

Timekiller
  • 2,946
  • 2
  • 16
  • 16