95

Here is my code snippet:

public void joinRoom(String room) throws MulticasterJoinException {
  String statusCheck = this.transmit("room", "join", room + "," + this.groupMax + "," + this.uniqueID);

  if (statusCheck != "success") {
    throw new MulticasterJoinException(statusCheck, this.PAppletRef);
  }
}

However for some reason, if (statusCheck != "success") is returning false, and thereby throwing the MulticasterJoinException.

5
  • Equals() vs ==. zparacha.com/java-string-comparison Commented Dec 13, 2011 at 5:21
  • leepoint.net/notes-java/data/strings/12stringcomparison.html Commented Dec 13, 2011 at 5:22
  • 7
    SO should start detecting this question and answering it on its own. Commented Dec 13, 2011 at 5:24
  • Didn't see an answer to this question in the suggestions as I entered in the title, so I figured it was unique. Guess not... Commented Dec 13, 2011 at 5:28
  • @KublaiKhan, I ask this question (String equality) during interviews. It's not often someone can answer when == works and when it doesn't (and how to make it work again). Commented Dec 13, 2011 at 5:29

7 Answers 7

241
if (!"success".equals(statusCheck))
Sign up to request clarification or add additional context in comments.

2 Comments

note that how Peter uses !"success".equals(statusCheck) rather than !statusCheck.equals("success"). This way you can avoid NullPointerException if statusCheck is null.
I'd suggest you also take a look on "*.equalsIgnoreCase(string)", just to make sure everything is fine.
44

== and != work on object identity. While the two Strings have the same value, they are actually two different objects.

use !"success".equals(statusCheck) instead.

1 Comment

This is better than the accepted answer because it gives explanation.
35

Sure, you can use equals if you want to go along with the crowd, but if you really want to amaze your fellow programmers check for inequality like this:

if ("success" != statusCheck.intern())

intern method is part of standard Java String API.

4 Comments

+1 from me. Although I'd prefer not to see this idiom in codebase that I have to maintain, it sure is useful to know what intern() is and how it works. (Using Google Guava's Interner makes this stuff more explicit)
Four years later I realize I should have said that I'd never use that in "real" code and I don't recommend anyone else do so either. There's probably some edge case where interning a string provides a slight performance improvement but the point of my answer was to educate and amuse.
Even assuming someone reading the code knows what "Intern()" does, they'll still be confused at why it's there, because they'll be trying to figure out why it was used.
@Tasgall, I agree - if there's ever a need for intern() then it's safe to say that the code should be well documented.
9

do the one of these.

   if(!statusCheck.equals("success"))
    {
        //do something
    }

      or

    if(!"success".equals(statusCheck))
    {
        //do something
    }

2 Comments

what is the difference between those 2 conditions?
Wrong! Don't you ever do that! (I mean, the first one). Why? if statusCheck==null, then you gonna get NPE in your face. In the second example you only get false is statusCheck is null. @AlicanBalik
4

Please use !statusCheck.equals("success") instead of !=.

Here are more details.

1 Comment

Actually you both @anubhava and SubOP are wrong. Never put a nullable object first in an equals(Object) method if you can. Should be !"success".equals(statusCheck). Otherwise you can get a NPE if statusCheck == null
4

You need to use the method equals() when comparing a string, otherwise you're just comparing the object references to each other, so in your case you want:

if (!statusCheck.equals("success")) {

2 Comments

equals, not Equals.
even better is to check for !"success".equals(statusCheck) in case statusCheck is null, will not throw a NullPointerException
2

you can use equals() method to statisfy your demands. == in java programming language has a different meaning!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.