85

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.

勿绮语
  • 9,170
  • 1
  • 29
  • 37
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195

7 Answers7

232
if (!"success".equals(statusCheck))
勿绮语
  • 9,170
  • 1
  • 29
  • 37
  • 71
    note that how Peter uses !"success".equals(statusCheck) rather than !statusCheck.equals("success"). This way you can avoid NullPointerException if statusCheck is null. – Alvin Feb 13 '12 at 04:27
  • 1
    I'd suggest you also take a look on "*.equalsIgnoreCase(string)", just to make sure everything is fine. – Malakai Nov 12 '16 at 15:20
40

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

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

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
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.

Paul
  • 19,704
  • 14
  • 78
  • 96
  • 1
    +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) – Peter Štibraný Feb 25 '12 at 19:16
  • 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. – Paul Sep 25 '15 at 12:39
  • 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 Feb 16 '16 at 01:54
  • @Tasgall, I agree - if there's ever a need for `intern()` then it's safe to say that the code should be well documented. – Paul Feb 16 '16 at 15:15
8

do the one of these.

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

      or

    if(!"success".equals(statusCheck))
    {
        //do something
    }
subodh
  • 6,136
  • 12
  • 51
  • 73
  • what is the difference between those 2 conditions? – Alican Balik Mar 28 '18 at 15:52
  • 3
    **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 – pawpaw Jul 17 '18 at 09:56
4

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

Here are more details.

lobster1234
  • 7,679
  • 26
  • 30
  • 2
    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` – pawpaw Jul 17 '18 at 09:59
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")) {
Matt Lacey
  • 8,227
  • 35
  • 58
2

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

zhaoyw
  • 31
  • 2