Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

8
  • 3
    Why not just change the String types to Objects - making it more generic? And then it is the same as what you will get if you move to Java 7.
    – Tom
    Commented Nov 7, 2013 at 18:31
  • 5
    Which java class contains method you provided? Commented Jul 25, 2014 at 13:58
  • 53
    Hey! you shouldn't have a compare method returning true/false. Equals is not the same as compare. Compare should be useful for sorting. You should return <0, ==0 or >0 to indicate which one is lower/grater than the other
    – coya
    Commented Dec 15, 2016 at 23:24
  • 29
    i suggest using Objects.equals(Object, Object) as Mark Rotteveel has pointed out in his answer (please upvote it)
    – Neuron
    Commented Jan 9, 2018 at 20:13
  • 1
    @hyperpallium the first thing, String.equals does internally, is a reference comparison, like if(this == other) return true;. Since String is final, even the simplest JVMs will be capable of inlining that code, not to speak of modern common implementations. So if the strings are identical (equal and interned), the early reference comparison doesn’t gain much and in all other cases, it doesn’t help at all, except for the scenario where both are null, perhaps.
    – Holger
    Commented May 25, 2020 at 10:20