Java Object Equal equals(Object thisObj, Object thatObj)

Here you can find the source of equals(Object thisObj, Object thatObj)

Description

Compares two objects.

License

Apache License

Parameter

Parameter Description
thisObj The left hand object to compare. May be an array or <code>null</code>
thatObj The right hand object to compare. May be an array or <code>null</code>

Return

true if two objects are equal; false otherwise.

Declaration

public static boolean equals(Object thisObj, Object thatObj) 

Method Source Code

//package com.java2s;
/**/*from  w w  w  .j a  va  2s.  co m*/
 * Copyright 2009-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import java.util.Arrays;

public class Main {
    /**
     * Compares two objects. Returns <code>true</code> if
     * <ul>
     * <li>{@code thisObj} and {@code thatObj} are both <code>null</code></li>
     * <li>{@code thisObj} and {@code thatObj} are instances of the same type and
     * {@link Object#equals(Object)} returns <code>true</code></li>
     * <li>{@code thisObj} and {@code thatObj} are arrays with the same component type and equals()
     * method of {@link Arrays} returns <code>true</code> (not deepEquals())</li>
     * </ul>
     * 
     * @param thisObj The left hand object to compare. May be an array or <code>null</code>
     * @param thatObj The right hand object to compare. May be an array or <code>null</code>
     * @return <code>true</code> if two objects are equal; <code>false</code> otherwise.
     */
    public static boolean equals(Object thisObj, Object thatObj) {
        if (thisObj == null) {
            return thatObj == null;
        } else if (thatObj == null) {
            return false;
        }
        final Class<?> clazz = thisObj.getClass();
        if (!clazz.equals(thatObj.getClass())) {
            return false;
        }
        if (!clazz.isArray()) {
            return thisObj.equals(thatObj);
        }
        final Class<?> componentType = clazz.getComponentType();
        if (long.class.equals(componentType)) {
            return Arrays.equals((long[]) thisObj, (long[]) thatObj);
        } else if (int.class.equals(componentType)) {
            return Arrays.equals((int[]) thisObj, (int[]) thatObj);
        } else if (short.class.equals(componentType)) {
            return Arrays.equals((short[]) thisObj, (short[]) thatObj);
        } else if (char.class.equals(componentType)) {
            return Arrays.equals((char[]) thisObj, (char[]) thatObj);
        } else if (byte.class.equals(componentType)) {
            return Arrays.equals((byte[]) thisObj, (byte[]) thatObj);
        } else if (boolean.class.equals(componentType)) {
            return Arrays.equals((boolean[]) thisObj, (boolean[]) thatObj);
        } else if (float.class.equals(componentType)) {
            return Arrays.equals((float[]) thisObj, (float[]) thatObj);
        } else if (double.class.equals(componentType)) {
            return Arrays.equals((double[]) thisObj, (double[]) thatObj);
        } else {
            return Arrays.equals((Object[]) thisObj, (Object[]) thatObj);
        }
    }
}

Related

  1. Equals(Object in1, Object in2)
  2. equals(Object o1, Object o2)
  3. equals(Object o1, Object o2)
  4. equals(Object o1, Object o2)
  5. equals(Object object1, Object object2)
  6. equals(Object v1, Object v2)
  7. equals(Object x, Object y)
  8. equals(Object... objects)
  9. internalEquals(Object[] o1, Object[] o2)