Signup/Sign In

Java Arrays equals() Method

In this tutorial, we will learn about the equals() method of the Arrays class in Java. This method is used to check if the given two arrays are equal or not. Both arrays are equal if both arrays are null or both arrays are having the same length and each corresponding element is the same. This method will accept two arrays and after comparing these two arrays it will return a boolean value true or false based on equality.

Syntax:

public static boolean equals(Object[] array1,Object[] aarray2)

List of Overloading Methods of equals() Method

This table contains all the overloaded variants of equals() method.

Method Description

static boolean equals(boolean[] a, boolean[] a2)

This method returns true if the two specified arrays of booleans are equal to one another.

static boolean equals(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)

This method returns true if the two specified arrays of booleans, over the specified ranges, are equal to one another.

static boolean equals(byte[] a, byte[] a2)

This method returns true if the two specified arrays of bytes are equal to one another.

static boolean equals(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)

This method returns true if the two specified arrays of bytes, over the specified ranges, are equal to one another.

static boolean equals(char[] a, char[] a2)

This method returns true if the two specified arrays of chars are equal to one another.

static boolean equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)

This method returns true if the two specified arrays of chars, over the specified ranges, are equal to one another.

static boolean equals(double[] a, double[] a2)

This method returns true if the two specified arrays of doubles are equal to one another.

static boolean equals(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)

This method returns true if the two specified arrays of doubles, over the specified ranges, are equal to one another.

static boolean equals(float[] a, float[] a2)

This method returns true if the two specified arrays of floats are equal to one another.

static boolean equals(float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)

This method returns true if the two specified arrays of floats, over the specified ranges, are equal to one another.

static boolean equals(int[] a, int[] a2)

This method returns true if the two specified arrays of ints are equal to one another.

static boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)

This method returns true if the two specified arrays of ints, over the specified ranges, are equal to one another.

static boolean equals(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)

This method returns true if the two specified arrays of longs, over the specified ranges, are equal to one another.

static boolean equals(long[] a, long[] a2)

This method returns true if the two specified arrays of longs are equal to one another.

static boolean equals(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)

This method returns true if the two specified arrays of shorts, over the specified ranges, are equal to one another.

static boolean equals(short[] a, short[] a2)

This method returns true if the two specified arrays of shorts are equal to one another.

static boolean equals(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex)

This method returns true if the two specified arrays of Objects, over the specified ranges, are equal to one another.

static boolean equals(Object[] a, Object[] a2)

This method returns true if the two specified arrays of Objects are equal to one another.

static <T> boolean equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)

This method returns true if the two specified arrays of Objects, over the specified ranges, are equal to one another.

Example of equals() Method

In the example below, we have two arrays array1 and array2. Both the corresponding array elements are equal therefore it will return true.

import java.util.Arrays; 
public class StudyTonight 
{ 
	public static void main(String[] args) 
	{ 
		int[] array1 = new int [] {1, 2, 3, 4}; 
		int[] array2 = new int [] {1, 2, 3, 4}; 
		if(Arrays.equals(array1, array2))
		{
			System.out.println("Both Arrays Are Equal");
		}
		else
		{
			System.out.println("Both Arrays Are Not Equal");
		}
	} 
} 


Both Arrays Are Equal

There is another overloading method of equals() which works on slicing an array. We need to pass start and end indexes for the comparison of any array.

Example of equals() Method with the slicing of an array

The example below is same as the above example but we can slice the arrays with passing startIndex and endIndex for both the arrays. For this example, we are comparing array1 and array2, from index 2 to 8 and from 4 to 10.

import java.util.Arrays; 
public class StudyTonight 
{ 
	public static void main(String[] args) 
	{ 
		int[] array1 = new int [] {1, 7, 9, 11, 21, 2, 3,9, 4, 12, 47, 85, 2, 62, 14, 72, 11, 4}; 
		int[] array2 = new int [] {1, 12, 41, 71, 22, 36, 4, 6, 12, 2, 12, 15, 16, 22, 18, 3, 4}; 
		if(Arrays.equals(array1,2,8,array2,4,10))
		{
			System.out.println("Both Arrays Are Equal");
		}
		else
		{
			System.out.println("Both Arrays Are Not Equal");
		}
	} 
} 


Both Arrays Are Not Equal

Example of equals() Method for the array of objects

The example below illustrates how equals() method can be applied to an array of objects also. Since this object is not primitive datatype we required to override an equals() method for our object, in our case we are Student is an object.

import java.util.Arrays;
class Student
{
	String stud_name;
	int roll_no;
	Student(String stud_name, int roll_no){
		this.stud_name = stud_name;
		this.roll_no = roll_no;
	}
	//overrifing equals method
	public boolean equals(Object obj) 
    {
        Student s = (Student)obj; 
        return (this.stud_name == s.stud_name && this.roll_no==s.roll_no); 
    } 
}
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		Student array1[][] = { 
				{ new Student("AAA",123), new Student("BBB",789) }, 
				{ new Student("CCC",183), new Student("DDD",445) }, 
				{ new Student("EEE",693), new Student("FFF",348) } }; 

		Student array2[][] = { 
				{ new Student("AAA",123), new Student("BBB",789) }, 
				{ new Student("CCC",183), new Student("DDD",445) }, 
				{ new Student("EEE",693), new Student("FFF",348) } }; 		

		if(Arrays.deepEquals(array1, array2))
		{
			System.out.println("Both arrays are equal");
		}
		else
		{
			System.out.println("Both arrays are not equal");
		}
	} 
}


Both arrays are not equal

Example of equals() Method All overloading methods

In the following example, we implemented all the overloading methods of equals() method. This method will return true if two arrays are equal otherwise it will return a false value.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		byte byteArray1[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60}; 
		byte byteArray2[] = {35, 40, 45, 50, 55, 60,5, 10, 15, 20, 25, 30}; 
		System.out.println("Arrays.equals(byteArray1,byteArray2): "+Arrays.equals(byteArray1,byteArray2));
		System.out.println("Arrays.equals(byteArray1, 0, 5, byteArray2, 0, 5): "+Arrays.equals(byteArray1, 0, 5, byteArray2, 0, 5));		
		
		char charArray1[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'}; 
		char charArray2[] = {'d', 'e', 'f', 'g', 'h', 'i','a', 'b', 'c', 'd', 'e'}; 
		System.out.println("Arrays.equals(charArray1,charArray2): "+Arrays.equals(charArray1,charArray2));
		System.out.println("Arrays.equals(charArray1, 0, 5, charArray2, 0, 5): "+Arrays.equals(charArray1, 0, 5, charArray2, 0, 5));	
		
		int intArray1[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150};
		int intArray2[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150};
		System.out.println("Arrays.equals(intArray1, intArray2): "+Arrays.equals(intArray1, intArray2));
		System.out.println("Arrays.equals(intArray1, 0, 5, intArray2, 0, 5): "+Arrays.equals(intArray1, 0, 5, intArray2, 0, 5));	
		
		double doubleArray1[] = {5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2}; 
		double doubleArray2[] = {6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2,5.1, 6.2, 7.2, 8.1, 9.4}; 
		System.out.println("Arrays.equals(doubleArray1, doubleArray2): "+Arrays.equals(doubleArray1, doubleArray2));
		System.out.println("Arrays.equals(doubleArray1, 0, 5, doubleArray2, 0, 5): "+Arrays.equals(doubleArray1, 0, 5, doubleArray2, 0, 5));	
		
		float floatArray1[] = {1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f}; 
		float floatArray2[] = {1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f}; 
		System.out.println("Arrays.equals(floatArray1, floatArray2): "+Arrays.equals(floatArray1, floatArray2));
		System.out.println("Arrays.equals(floatArray1, 0, 5, floatArray2, 0, 5): "+Arrays.equals(floatArray1, 0, 5, floatArray2, 0, 5));	
		
		short shortArray1[] = {12, 14, 16, 18, 20, 2, 4, 6, 8}; 
		short shortArray2[] = {2, 4, 6, 8, 10, 3, 12, 5, 32, 16}; 
		System.out.println("Arrays.equals(shortArray1, shortArray2): "+Arrays.equals(shortArray1, shortArray2));
		System.out.println("Arrays.equals(shortArray1, 0, 5, shortArray2, 0, 5): "+Arrays.equals(shortArray1, 0, 5, shortArray2, 0, 5));	
	}
}


Arrays.equals(byteArray1,byteArray2): false
Arrays.equals(byteArray1, 0, 5, byteArray2, 0, 5): false
Arrays.equals(charArray1,charArray2): false
Arrays.equals(charArray1, 0, 5, charArray2, 0, 5): false
Arrays.equals(intArray1, intArray2): true
Arrays.equals(intArray1, 0, 5, intArray2, 0, 5): true
Arrays.equals(doubleArray1, doubleArray2): false
Arrays.equals(doubleArray1, 0, 5, doubleArray2, 0, 5): false
Arrays.equals(floatArray1, floatArray2): true
Arrays.equals(floatArray1, 0, 5, floatArray2, 0, 5): true
Arrays.equals(shortArray1, shortArray2): false
Arrays.equals(shortArray1, 0, 5, shortArray2, 0, 5): false

Conclusion

In this tutorial, we learned about the equal() method from the Arrays class. This method comes with a two variant on is without slicing public static boolean equals(Object[] array1,Object[] aarray2) and other is with slicing public static boolean equals(Object[] array1,int FromIndex,int ToIndex,Object[] array2,int FromIndex,int ToIndex).



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.