== or equal when comparing final static fields

I returned to Java after a short break from C # and I was already tired of writing things like a.equals(b)

, so I was wondering if there was a way to use the operator ==

without breaking my program.

Here is my situation:

public static class A {
    public final static A STATIC_FIELD = new A(prop1, prop2);

    public A method(int param){
        return (param > 0.5) ? STATIC_FIELD : new A(prop1, prop2);
    }
}

      

Now, can I replace equals

with ==

in the following snippet because I mean the same object or is it wrong?

....
private bool method(){
    A aInstance = getAInstance();
    int param = Math.Random();
    return aInstance.method(param).equals(A.STATIC_FIELD);
}
....

      

And what if STATIC_FIELD

in a meaning taken from enum

?

+3


source to share


5 answers


It depends on what you want to do.

If you need to check if this is the exact same object, use direct comparison. If you need to check if an object carries the same content (like string or number) then .equals should be used.



The most common mistakes with these methods appear from string comparison, where .equals returns true if the strings have the same content, but == only returns true if they are the same string instance.

+3


source


You should avoid == (okay, admit you know that :-)), also with singleton instances, because later on you might "forget" that it is being instantiated via a singleton. But I'll give you a hint. Instead:

if ((var != null) && (var.equals(A.STATIC_FIELD)))

      

You can write:

if (A.STATIC_FIELD.equals(var))

      



just as a lot of people don't understand that to compare strings they can use:

if ("static string".equals(varString))

      

You can also use java.util.Objects.equals to compare objects without checking for this null value.

+1


source


If you don't override equals () equals()

and ==

do the same, check for reference equality. You can make a substitution in this case, since the () method will return the same reference as A.STATIC_FIELD when it is true. See this answer .

0


source


no you cannot use == instead of equals () because when we use == in java we are actually comparing the memory address of the object, so if the method returns STATIC_FIELD then it will work correctly as its static object and the address will be like this the same everywhere. But when the method returns a new object A, then the address will not match even if the content is the same in the class, because the new A (param1, param2) insists on the JVM to create a new object in a different memory location

0


source


==

checks if two objects are the same instance. If you want to check if two variables contain the same instance (as it might if you assign variables to class constants) is ==

fine for use. It also won't be an error if your variable holds null

, so it takes precedence over .equals()

.

Instances are enum

always constant and two different instances will never be equal, so ==

works great for them.

0


source







All Articles