0

Possible Duplicate:
Equal strings aren't equal (==) in Java?

From Eclipse's Display window:

 messages.get(i).getMsg() == lastMsg
 (boolean) false
 messages.get(i).getMsg().length()
 (int) 14
 lastMsg.length()
 (int) 14
 messages.get(i).getMsg()
 (java.lang.String) INSERT QUARTER
 lastMsg
 (java.lang.String) INSERT QUARTER

Fairly new to Java. How can the first statement be false? Am I missing something ridiculously simple here?

Community
  • 1
  • 1
AngryHacker
  • 59,598
  • 102
  • 325
  • 594

7 Answers7

5

When you compare strings in Java, you should do that by calling the equals() method, not by using the == operator.

The == operator does not test if the value or content of two objects is the same. It simply checks if the two expressions on both sides of the == refer to the exact same object. If you have two String objects that have the same content, but that are distinct objects, then == will return false.

(There is a minor complication to this though: the compiler does some clever tricks so that if you use string literals, == will actually return true).

String msg = messages.get(i).getMsg();
System.out.println(msg == lastMsg); // false
System.out.println(msg.equals(lastMsg)); // true
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • If you use string literals, they will end up as the same object not only thanks to clever compiler tricks, but as a result of the language spec. – Thilo Dec 27 '11 at 08:13
  • Yes @Thilo, I didn't want to go into that too much here as it would probably just confuse AngryHacker. – Jesper Dec 27 '11 at 08:14
1

You're comparing strings with == - which means you're actually seeing whether the two string references are references to the same string object.

It looks like you've actually got two distinct string objects which are equal - so you need to use equals:

messages.get(i).getMsg().equals(lastMsg) // Should print true

This is a very common mistake for those new to Java, so don't feel bad :) Operators can't be overloaded in Java, so for reference types, == always compares the references themselves, a sort of identity comparison rather than an equivalence comparison.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

== compares the references and not the string content. If you want to compare strings in java use String.equals:

lastMsg.equals(messages.get(i).getMsg())
Howard
  • 38,639
  • 9
  • 64
  • 83
1

When dealing with String in Java and checking for equality you should be using equals and not ==

Eugene
  • 117,005
  • 15
  • 201
  • 306
1

"==" operator compares objects' references. use method equals() to compare objects themselves

Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34
1

So it looks like messages.get(i).getMsg() returns a String and lastMsg is also a String. If you use the == to test equality, you're actually comparing the instances and it's returning false because the 2 Strings are 2 different instances eventhough the String that they represent are the same. You need to use Object's .equals() method to compare the quality of Objects. See also: Java String.equals versus ==

Community
  • 1
  • 1
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
1

If you are coming from the C/C++ world, then this is a classic Java "gotcha".

The comparison operator (==) is generally "true" when the objects on both sides of the == are the same object, and not because the members of both objects are equivalent.

selbie
  • 100,020
  • 15
  • 103
  • 173