Java Does Not Equal (!=) Not Working? [duplicate]

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.


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

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

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


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.


do the one of these.

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

      or

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

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

Here are more details.