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?
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 twoStringobjects that have the same content, but that are distinct objects, then==will returnfalse.(There is a minor complication to this though: the compiler does some clever tricks so that if you use string literals,
==will actually returntrue).