Java Object Equal equals(final Object a, final Object b)

Here you can find the source of equals(final Object a, final Object b)

Description

Check whether two objects are equal

License

LGPL

Parameter

Parameter Description
a the first object
b the second object

Return

true only if the two objects are equal, false otherwise

Declaration

public static final boolean equals(final Object a, final Object b) 

Method Source Code


//package com.java2s;
/*/* w w w.j av a2s . c o m*/
 * Copyright (c) 2010 Thomas Weise
 * http://www.it-weise.de/
 * tweise@gmx.de
 *
 * GNU LESSER GENERAL PUBLIC LICENSE (Version 2.1, February 1999)
 */

import java.util.Arrays;

public class Main {
    /**
     * Check whether two objects are equal
     *
     * @param a
     *          the first object
     * @param b
     *          the second object
     * @return true only if the two objects are equal, false otherwise
     */
    public static final boolean equals(final Object a, final Object b) {
        Class<?> c;

        if (a == b) {
            return true;
        }
        if ((a == null) || (b == null)) {
            return false;
        }

        c = a.getClass();
        if ((c == b.getClass()) && (c.isArray())) {
            if (c == byte[].class) {
                return Arrays.equals(((byte[]) a), ((byte[]) b));
            }
            if (c == short[].class) {
                return Arrays.equals(((short[]) a), ((short[]) b));
            }
            if (c == int[].class) {
                return Arrays.equals(((int[]) a), ((int[]) b));
            }
            if (c == long[].class) {
                return Arrays.equals(((long[]) a), ((long[]) b));
            }
            if (c == char[].class) {
                return Arrays.equals(((char[]) a), ((char[]) b));
            }
            if (c == boolean[].class) {
                return Arrays.equals(((boolean[]) a), ((boolean[]) b));
            }
            if (c == float[].class) {
                return Arrays.equals(((float[]) a), ((float[]) b));
            }
            if (c == double[].class) {
                return Arrays.equals(((double[]) a), ((double[]) b));
            }
            return Arrays.deepEquals(((Object[]) a), ((Object[]) b));
        }

        return (a.equals(b));
    }
}

Related

  1. areEqual(Object o1, Object o2)
  2. areEqualStr(Object o1, Object o2)
  3. deepEquals(Object o1, Object o2)
  4. equalObjects(Object object1, Object object2)
  5. equals(BSONObject a, BSONObject b)
  6. equals(Object a, Object b)
  7. Equals(Object in1, Object in2)
  8. equals(Object o1, Object o2)
  9. equals(Object o1, Object o2)