46

I am aware that it is better to call the equals method over using the == operator (see this question). I want two strings to compare as equal if they are both null or if they represent the same string. Unfortunately the equals method will throw an NPE if the strings are null. My code is currently:

boolean equals(String s1, String s2) {
  if (s1 == null && s2 == null) {
    return true;
  }
  if (s1 == null || s2 == null) {
    return false;
  }
  return s1.equals(s2);
}

This is inelegant. What is the correct way to perform this test?

5
  • How is it inelegant? What other way to determine? Commented May 6, 2015 at 15:46
  • Use StringUtils.equals(s1, s2) in commons.lang
    – thoitbk
    Commented May 6, 2015 at 15:47
  • 2
    @fge I've never understood why people post answers as comments. I'd like to know though, care to share your reasoning? Commented May 7, 2015 at 9:02
  • @Pureferret no particular reason, really... Out of habit when the answer really is only a reference to an existing method :p
    – fge
    Commented May 7, 2015 at 10:51
  • 1
    @fge it just seems to me that it either ends up as a) and answer or b) lost as a comment, with no further (helpful) explanation. Commented May 7, 2015 at 10:57

3 Answers 3

84

If Java 7+, use Objects.equals(); its documentation explicitly specifies that:

[...] if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

which is what you want.

If you don't, your method can be rewritten to:

return s1 == null ? s2 == null : s1.equals(s2);

This works because the .equals() contract guarantees that for any object o, o.equals(null) is always false.

8
  • 3
    The code inside Objects.equals() is return (a == b) || (a != null && a.equals(b));. I guess this would be a better way to do it prior to Java 7 since it's straight from the horses mouth. Commented May 6, 2015 at 15:53
  • 2
    @SashaSalauyou I saw that. It's ironic that your answer got 3 downvotes when it was the most elegant one. Unfortunately, these things happen and we have to move on :) +1 from me if it is undeleted Commented May 6, 2015 at 16:00
  • @ChetanKinger the code of Objects.equals() is really a micro optimization at this point; it basically checks the reference equality first, which happens to work for null (since null == null is true) and then checks that a is not null, in which case it calls .equals(); my code really does not differ much, really. And I don't understand the downvote at all.
    – fge
    Commented May 6, 2015 at 18:37
  • @fge I did not downvote you. There was no need to to downvote this answer. I always leave a comment when I downvote an answer. Can the downvoter please leave a comment so that the OP can know what's wrong with the answer? Commented May 7, 2015 at 2:59
  • @ChetanKinger I don't know how .equals is implemented. However the linked documentation says what it will do. The code suggested in this answer is slightly closer to what that documentation says than your code is. However it would only make a difference for a class in which a.equals(a) could return false, which would seem silly.
    – kasperd
    Commented May 7, 2015 at 17:52
44

From Objects.equals():

return (a == b) || (a != null && a.equals(b));

Very simple, self-explaining and elegant.

1
  • 3
    I like this. If a and b are the same reference, it returns true immediately.
    – Navin
    Commented May 7, 2015 at 7:42
4

If you can't use Java 7+ solution, but you have Guava or Commons Lang in classpath, then you can use the following:

Guava:

import com.google.common.base.Objects;

Objects.equal(s1, s2);

Commons Lang:

import org.apache.commons.lang3.builder.EqualsBuilder;

new EqualsBuilder().append(s1, s2).isEquals();

or

import org.apache.commons.lang3.StringUtils;

StringUtils.equals(s1, s2);
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.