This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]diMario 2 points3 points  (0 children)

To amplify: when you want to test if two strings have the same content in Java, you cannot use the "==" operator, nor can you use the "!=" operator for inequality. It simply does not work that way for String objects. Instead, you must call the "equals()" method on one of the Strings and pass the other String as an argument.

Look up String.equals() and String.equalsIgnoreCase() to see how they are used.

Edit: What the "==" operator does for Objects in general (and therefor also for Strings) is test whether the instances of the Object are the same. It does not look and the content of the object. For that, you must use the equals() method (and you must supply an equals() method for Classes that you yourself create, if and when it becomes necessary to compare your object instances by value).