Table of Contents

    Java equals() Method: Usage and Examples

    Java equals() Method: Usage and Examples

    Description

    The method determines whether the Number object that invokes the method is equal to the object that is passed as an argument.

    Syntax

    public boolean equals(Object o)

    Parameters

    Here is the detail of parameters ?

    • Any object.

    Return Value

    • The method returns True if the argument is not null and is an object of the same type and with the same numeric value. There are some extra requirements for Double and Float objects that are described in the Java API documentation.

    Example

    public class EqualsMethod {
    
       public static void main(String args[]) {
          Integer x = 10;
          Integer y = 20;
          Integer z =10;
          Short a = 10;
    
          System.out.println(x.equals(y));
          System.out.println(x.equals(z));
          System.out.println(x.equals(a));
       }
    }

    Output

    false
    true
    false
    Press any key to continue . . .
    Methods Description
    boolean equals(Object object) Determines whether one object is equal to another.
    boolean contentEquals(CharSequence str) Returns true if the invoking string contains the same string as
    str. Otherwise, returns false. Added by J2SE 5.
    boolean equals(Object FloatObj Returns true if the invoking Float object is equivalent to
    FloatObj. Otherwise, it returns false.
    boolean equals(Object DoubleObj )  Returns true if the invoking Double object is equivalent
    to DoubleObj. Otherwise, it returns false.
    boolean equals(Object ByteObj  Returns true if the invoking Byte object is
    equivalent to ByteObj. Otherwise, it returns false.
    boolean equals(Object ShortObj ) Returns true if the invoking Short object is equivalent
    to ShortObj. Otherwise, it returns false.
    boolean equals(Object IntegerObj ) Returns true if the invoking Integer object is
    equivalent to IntegerObj. Otherwise, it returns false.
    boolean equals(Object LongObj  Returns true if the invoking Long object is equivalent
    to LongObj. Otherwise, it returns false.
    boolean equals(Object boolObj)  Returns true if the invoking object is equivalent to
    boolObj. Otherwise, it returns false.
    boolean equals(Object object) Returns true if the invoking object is equivalent to
    object.
    boolean equals(Object ob ) Returns true if the invoking StackTraceElement is the same as the
    one passed in ob. Otherwise, it returns false.
    final boolean equals(Object obj ) Returns true if obj and the invoking object refer
    to the same constant.
    boolean equals(Object bitSet ) Returns true if the invoking bit set is equivalent to the one passed in bitSet. Otherwise, the method returns false.
    boolean equals(Object date) Returns true if the invoking Date object contains the same time and date as the one specified by date. Otherwise, it returns false.
    boolean equals(Object calendarObj ) Returns true if the invoking Calendar object contains a date that is equal to the one specified by calendarObj. Otherwise, it returns false.
    boolean equals(Object other) Returns true if this object has the same Internet address as other.
    boolean equals(Object FontObj ) Returns true if the invoking object contains the same font as that specified by FontObj. Otherwise, it returns false.

    The equals( ) method returns true if two arrays are equivalent. Otherwise, it returns false. The equals( ) method has the following forms:

    static boolean equals(boolean array1[ ], boolean array2[ ])
    static boolean equals(byte array1[ ], byte array2[ ])
    static boolean equals(char array1[ ], char array2[ ])
    static boolean equals(double array1[ ], double array2[ ])
    static boolean equals(float array1[ ], float array2[ ])
    static boolean equals(int array1[ ], int array2[ ])
    static boolean equals(long array1[ ], long array2[ ])
    static boolean equals(short array1[ ], short array2[ ])
    static boolean equals(Object array1[ ], Object array2[ ])

    Here, array1 and array2 are the two arrays that are compared for equality.