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")
?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With