Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight when equality operator (==) is used for string comparisons in Eclipse

Is there any way I can get Eclipse to highlight the use of the == operator to test String equality? I keep mistakenly using it instead of calling .equals().

I'd really like to make that into a warning and require an @SuppressWarnings annotation to remove it, in the yet-to-happen case that I actually want to compare strings for object equality.

Are there any tools can I use to help break this bad habit at edit-time?

like image 680
Tom Tresansky Avatar asked Jul 13 '10 13:07

Tom Tresansky


People also ask

When would you use the == operator to compare instances of string?

Using == operator: == operator used to check the reference equality of the two strings, whether they are pointing towards the same string object. Using compareTo() method: compareTo() method used to check the strings lexicographically, i.e. alphabetically.

Can we compare string using equality operator operator?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

What happens when you compare two strings with the == operator?

String Comparison With Objects Class The method returns true if two Strings are equal by first comparing them using their address i.e “==”. Consequently, if both arguments are null, it returns true and if exactly one argument is null, it returns false.

When using the == operator with strings What is Java comparing?

The Java equals() method compares two string objects, the equality operator == compares two strings, and the compareTo() method returns the number difference between two strings. String comparison is a crucial part of working with strings in Java.


2 Answers

Use a static analysis tool such as FindBugs, PMD, or CheckStyle.

There are Eclipse plugins for each, along with Ant tasks, Maven plugins, etc.

Each of these has rules relating to String equality (Findbugs rule, PMD rule, Checkstyle rule).

like image 132
matt b Avatar answered Oct 13 '22 01:10

matt b


The obvious answer to the question has already been given, but here is a warning that is not a direct answer: obj.equals can also fail if obj is null. So you'll often have to use code like this:

if(mystr1 != null && mystr1.equals(mystr2))

because this

if(mystr1.equals(mystr2))

would fail with a NullPointerException if mystr1 is null.

Which is why, when the comparison string is a known constant, the following syntax is often used:

if("ABCDEF".equals(mystr1))

rather than

if(mystr1.equals("ABCDEF"))

For this reason, many libraries (like apache commons / lang ) provide utility functions that combine these checks:

// this is the definition of org.apache.commons.lang.StringUtils.equals(String, String)
public static boolean equals(String str1, String str2) {
    return str1 == null ? str2 == null : str1.equals(str2);
}

// this is the definition of  org.apache.commons.lang.ObjectUtils.equals(Object, Object)
public static boolean equals(Object object1, Object object2) {
    if (object1 == object2) {
        return true;
    }
    if ((object1 == null) || (object2 == null)) {
        return false;
    }
    return object1.equals(object2);
}

Using these methods is usually safer than plain equals, unless you know for sure that one of the two objects is not null

like image 20
Sean Patrick Floyd Avatar answered Oct 13 '22 01:10

Sean Patrick Floyd