2

I am confused about the following code. Why does it compare with both == and equals method?

(validFolderRow.getBondTAFolderType() == null || validFolderRow.getBondTAFolderType().equals("null"))

What's the difference between equals() and ==?

Can anyone tell me what is the difference between

validFolderRow.getBondTAFolderType() == null

and

validFolderRow.getBondTAFolderType().equals("null")

?

Community
  • 1
  • 1
Sitansu
  • 3,225
  • 8
  • 34
  • 61
  • 1
    This looks like a workaround for (or maybe part of) some really awkward code. – biziclop Jun 01 '15 at 09:38
  • 1
    It prevents a NPE if `validFolderRow.getBondTAFolderType()` is `null`, otherwise you will call equals on a null reference. – Alexis C. Jun 01 '15 at 09:38
  • Your question is very confusing... I don't see the relevance in linking the other question, or even the need for your first paragraph at all – musefan Jun 01 '15 at 09:45

5 Answers5

10

validFolderRow.getBondTAFolderType() == null compares to null (i.e. checks if validFolderRow.getBondTAFolderType() is null). validFolderRow.getBondTAFolderType().equals("null") compares validFolderRow.getBondTAFolderType() to a String whose value is "null".

Note that first comparison must be done first, since if validFolderRow.getBondTAFolderType() is null, you can't call equals on it (since it will throw a NullPointerException). Since || is a short circuit operator, evaluating the first operand to true will prevent the second operand from being evaluated.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

There is a massive difference...

.equals() calls the method which can theoretically be overridden and in the case of String compares the contents of the strings in memory, it checks if each variable contains the the same sequence of characters.

== checks if the variables point to the same location in memory.

In a lot of cases an == check on two String variables will evaluate to false, even if the Strings contain the same characters

In your particular case, one is actually checking the validFolderRow.getBondTAFolderType() method returns a null response (Nothing), by seeing if teh variable is not actually pointing at a memory location at all, and then if that is not the case it checks if it returns a String containing the character sequence null.

Matt Fellows
  • 6,512
  • 4
  • 35
  • 57
0
validFolderRow.getBondTAFolderType() == null 

Check's if the reference returned by getBondTAFolderType() is null

validFolderRow.getBondTAFolderType().equals("null")

Checks if the string returned by getBondTAFolderType() is equals to the string "null". This is similar to

String nullString = "null";
validFolderRow.getBondTAFolderType().equals(nullString);
Sajith Eshan
  • 696
  • 4
  • 17
0

validFolderRow.getBondTAFolderType() == null -> comparesto null . validFolderRow.getBondTAFolderType().equals("null") -> if validFolderRow.getBondTAFolderType() is null then you will get the NPE because you can't execute a method on null reference.

Sitansu
  • 3,225
  • 8
  • 34
  • 61
Shriram
  • 4,343
  • 8
  • 37
  • 64
0

Of course there is.

null is not technically a String;

"null" is a String.

If you want to compare to both cases, you'll probably need

String folderType = validFolderRow.getBondTAFolderType();
if(folderType != null && !"null".equals(folderType)) {
    ...
}

And there's of course also that == compares references, while .equals() compares content.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428