[Solved] Java , The equals() Method [closed]


Result of equals method depends very much on its implementation.

Method equals of Object:

public boolean equals(Object obj) {
    return (this == obj);
}

This means, that equals will return true, only if the two variables holds the references (therefore references to the same object).

If it returns false, this must by caused by overriding the original method.

I don’t know, where is the bug, because I don’t see your equals method, but I can give you an example, how to write one:

public class MyClass {

    private int integer;

    private String string;

    private char character;

    public MyClass(int integer, String string, char character) {
        this.integer = integer;
        this.string = string;
        this.character = character;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj == this)
            return true;

        if (!(obj instanceof MyClass))
            return false;

        MyClass myClass = (MyClass) obj;

        if (integer == myClass.integer && string.equals(myClass.string) && character == myClass.character)
            return true;
        else
            return false;
    }

}

I hope this helps you to find out mistake…

solved Java , The equals() Method [closed]