java.lang.Object.equals(Object obj)

Visual Studio 2005

Determines whether two objects are equal.


public boolean equals(Object obj)

Parameters

Parameter Description

obj

An instance of an Object to compare against the current Object for equality.

true if the two objects are equal; false otherwise.

// object_equals.jsl

public class MyObject extends Object
{
    public static void main(String[] args)
    {
        // Create three instance of MyObject objects.
        MyObject obj1 = new MyObject("Hello");
        MyObject obj2 = new MyObject("World");
        MyObject obj3 = new MyObject("World");

        // Is obj1 equal to obj2?
        boolean equality = obj1.equals(obj2);
        System.out.println("obj1 equals obj2? " +
            equality);

        // Is obj2 equal to obj3?
        equality = obj2.equals(obj3);
        System.out.println("obj2 equals obj3? " +
            equality);
    }

    public MyObject(String s)
    {
        aString = s;
    }

    // Two MyObject objects are equal if the aString
    // fields are equal.
    public boolean equals(Object o)
    {
        if (o instanceof MyObject)
        {
            MyObject myO = (MyObject)o;
            if (aString == myO.aString)
            {
                return true;
            }
        }

        return false;
    }

    private String aString = "";
}

/*
Output:
obj1 equals obj2? false
obj2 equals obj3? true
*/

Community Additions

ADD
Show: