Showing posts with label equals. Show all posts
Showing posts with label equals. Show all posts

Sunday, 5 April 2020

Java: equals() vs Arrays.equals

array1.equals(array2) method return true, if both the array references point to same object (same as array1 == array2), else false.

Arrays.equals() method is available in below forms, it return true if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.
public static boolean equals(byte[] a, byte[] a2)
public static boolean equals(short[] a, short[] a2)
public static boolean equals(int[] a, int[] a2)
public static boolean equals(long[] a, long[] a2)
public static boolean equals(float[] a, float[] a2)
public static boolean equals(double[] a, double[] a2)
public static boolean equals(char[] a, char[] a2)
public static boolean equals(boolean[] a, boolean[] a2)
public static boolean equals(Object[] a, Object[] a2)

App.java
package com.sample.app;

import java.util.Arrays;

public class App {

 public static void main(String args[]) {
  int[] data1 = { 2, 3, 5, 7 };
  int[] data2 = { 2, 3, 5, 7 };
  int[] data3 = data1;

  System.out.println("data1.equals(data2) : " + data1.equals(data2));
  System.out.println("data1.equals(data3) : " + data1.equals(data3));
  
  System.out.println("Arrays.equals(data1, data2) : " + Arrays.equals(data1, data2));
  System.out.println("Arrays.equals(data1, data3) : " + Arrays.equals(data1, data3));

 }

}

Output
data1.equals(data2) : false
data1.equals(data3) : true
Arrays.equals(data1, data2) : true
Arrays.equals(data1, data3) : true

Note
'Arrays.equals()'' do not work as expected for multidimensional arrays, it compares items of the 1st dimension for reference equality.

You may like

Thursday, 23 May 2019

Java: String equals vs compareTo


We can figure out the differences between equals and comapreTo methods, by looking at the source code.

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

public int compareTo(String anotherString) {
    int len1 = value.length;
    int len2 = anotherString.value.length;
    int lim = Math.min(len1, len2);
    char v1[] = value;
    char v2[] = anotherString.value;

    int k = 0;
    while (k < lim) {
        char c1 = v1[k];
        char c2 = v2[k];
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2;
}

a. From the source code, it is clear that, compareTo method compares two strings lexicographically (based on unicode value of each character in the strings), where as equals method checks whether two strings have same content or not.

b. str1.equals((String)null) return false, where as str1.compareTo((String)null) throws NullPointerException


App.java

package com.sample.app;

public class App {
    public static void main(String args[]) {
        String str1 = "Hello World";
        
        System.out.println("str1.equals((String)null) : " + str1.equals((String)null));
        System.out.println("str1.compareTo((String)null) : " + str1.compareTo((String)null));
    }
}

Output
str1.equals((String)null) : false
Exception in thread "main" java.lang.NullPointerException
    at java.lang.String.compareTo(String.java:1155)
    at com.sample.app.App.main(App.java:8)


You may like