Java Array equals Method

The Java Arrays.equals Method is one of the Java Array Methods, which is to check whether user-specified arrays are equal or not. If they are equal, it returns Boolean TRUE otherwise FALSE.

In this article, we will show how to compare the Arrays using Java Array equals method with an example. The syntax of the Arrays.equals in Java Programming language is as shown below.

Java Array equals Method syntax

There are nine different Java array equals methods to compare the user-specified Arrays. The following method will accept two Boolean Arrays (a1 and a2) as the parameters and check whether a1, a2 are equal or Not. If they are equal, it returns boolean TRUE otherwise FALSE.

public static boolean equals(Boolean[] al, Boolean[] a2); // It will return Boolean

//In order to use in program
Arrays.equals(Boolean[] al, Boolean[] a2);

This Java array equals method accepts two Byte Arrays (b1 and b2) and check whether b1, b2 are equal. If they are equal, it returns boolean TRUE otherwise FALSE.

public static boolean equals(Byte[] bl, Byte[] b2); // It will return Boolean

//In order to use in program
Arrays.equals(Byte[] bl, Byte[] b2);

This array equals method accept two Short Arrays (s1 and s2) as the parameters and check whether s1, s2 are equal or Not. If they are equal, it returns boolean TRUE otherwise FALSE.

public static boolean equals(short[] sl, short[] s2); // It will return Boolean

//In order to use in program
Arrays.equals(short[] sl, short[] s2);

Below array.equals method accept two Character Arrays (c1 and c2) as the parameters and check whether c1, c2 are equal or Not. If they are equal, it returns boolean TRUE otherwise FALSE.

public static boolean equals(char[] cl, char[] c2); // It will return Boolean

//In order to use in program
Arrays.equals(char[] cl, char[] c2);

This Java array equals method will accept two Integer Arrays (i1 and i2) as the parameters and check whether i1, i2 is equal or Not. If they are equal, it returns boolean TRUE otherwise FALSE.

public static boolean equals(int[] il, int[] i2); // It will return Boolean

//In order to use in program
Arrays.equals(int[] il, int[] i2);

It accepts two Long Arrays (l1 & l2) as the arguments and checks whether l1, l2 are equal or Not. If they are equal, it returns boolean TRUE otherwise FALSE.

public static boolean equals(long[] ll, long[] l2); // It will return Boolean

//In order to use in program
Arrays.equals(long[] ll, long[] l2);

This Java array equals method will accept two Double Arrays (d1 and d2) as the parameters and check whether d1, d2 are equal or Not. If they are equal, it returns boolean TRUE otherwise FALSE.

public static boolean equals(double[] dl, double[] d2); // It will return Boolean

//In order to use in program
Arrays.equals(double[] dl, double[] d2);

The following Java method will accept two Float Arrays (f1 and f2) as the parameters and check whether f1, f2 are equal or Not. If they are equal, it returns boolean TRUE otherwise FALSE.

public static boolean equals(float[] fl, float[] f2); // It will return Boolean

//In order to use in program
Arrays.equals(float[] fl, float[] f2);

It accepts two Object Arrays (Obj1 and Obj2) as the parameters and checks whether Obj1, Obj2 are equal or Not. If they are equal, it returns boolean TRUE otherwise FALSE.

public static boolean equals(Object[] Objl, Object[] Obj2); // It will return Boolean

//In order to use in program
Arrays.equals(Object[] Objl, Object[] Obj2);

Java equals to compare Byte Arrays

In this Java program, we declared three byte Arrays with random elements. Next, we will call the public static boolean equals (byte[] b1, byte[] b2) method to check whether they are equal or not.

package ArrayMethods;
import java.util.Arrays;

public class ByteEquals {
	public static void main(String[] args) {
		byte[] byteArray = {8, 20, 45, 12}; 
		byte[] bytArray = {8, 20, 45, 12}; 
		byte[] bitArray = {10, 25, 8, 19, 16}; 
		
		//Checking for Equality
		System.out.println(Arrays.equals(byteArray, bitArray));
				
		if(Arrays.equals(byteArray, bytArray)) {
			System.out.println("Two Byte Arrays are Equal");
		}
		else  {
			System.out.println("Byte Arrays are Not Equal");
		}
	}
}
Java Array Equals Method 1

First, we declared three byte Arrays and assigned some random values as the elements.

Next, we used the Arrays.equals method to check whether the above-specified byte arrays (byteArray, bitArray) are equal or not.

Next, we used the If Else Statement to check whether the above-specified byte arrays (byteArray, bitArray) are equal or not.

  • If the statement inside the If is True (If they are equal), then the System.out.println(” Two Byte Arrays are Equal “); statement printed.
  • Else System.out.println(“Byte Arrays are Not Equal”); statement will print.

Java equals to compare Boolean Arrays

In this Array equals program, we declared three boolean arrays with random elements. Then we will call the public static boolean equals (boolean[] a1, boolean[] a2) method to check whether they are equal or not.

package ArrayMethods;
import java.util.Arrays;

public class BooleanEquals {
	public static void main(String[] args) {
		boolean[] bool = {true, false, false, true};
		boolean[] boolArray = {true, false, true, false};
		boolean[] anBoolArray = {true, false, false, true};
		
		//Checking for Equality
		System.out.println(Arrays.equals(bool, boolArray));
				
		if(Arrays.equals(bool, anBoolArray)) {
			System.out.println("Both are Equal");
		}
		else  {
			System.out.println("Not Equal");
		}
	}
}
false
Both are Equal

Java equals to compare Short Arrays

In this array equals program, we declared three short arrays with random elements. Then we will call the public static boolean equals (short[] s1, short[] s2) method to check whether they are equal or not.

package ArrayMethods;
import java.util.Arrays;

public class ShortEquals {
	public static void main(String[] args) {
		short[] ShtArray = {5, 9, 11, 7, 6}; 
		short[] ShrtArray = {5, 9, 11, 7, 6}; 
		short[] ShortArray = {12, 6, 8, 3, 2}; 
		
		//Checking for Equality
		System.out.println(Arrays.equals(ShtArray, ShrtArray));
				
		if(Arrays.equals(ShrtArray, ShortArray)) {
			System.out.println("Both are Equal");
		}
		else  {
			System.out.println("Not Equal");
		}
	}
}
true
Not Equal

Java equals to compare Integer Arrays

In this Java program, we declared three integer arrays. Then we will call the public static boolean equals (int[] i1, int[] i2) method to check whether they are equal or not.

package ArrayMethods;
import java.util.Arrays;

public class IntEquals {
	public static void main(String[] args) {
		int[] intArray = {28, 64, 48, 95, 15}; 
		int[] itArray = {28, 64, 48, 95, 15}; 
		int[] anIntArray = {28, 64, 48, 105, 125}; 
		
		//Checking for Equality
		System.out.println(Arrays.equals(intArray, anIntArray));
				
		if(Arrays.equals(intArray, itArray)) {
			System.out.println("Both are Equal");
		}
		else  {
			System.out.println("Not Equal");
		}
	}
}
false
Both are Equal

compare Long Arrays

In this Java program, we declared three long arrays with random elements. Then we will call the public static boolean equals (long[] l1, long[] l2) method to check whether they are equal or not.

package ArrayMethods;
import java.util.Arrays;

public class LongEquals {
	public static void main(String[] args) {
		long[] LnArray = {164, 17, 198, 178, 135}; 
		long[] LngArray = {164, 17, 198, 178, 135}; 
		long[] LongArray = {250, 100, 80, 160, 190}; 	
		
		//Checking for Equality
		System.out.println(Arrays.equals(LnArray, LongArray));
				
		if(Arrays.equals(LnArray, LngArray)) {
			System.out.println("Both the Long Arrays are Equal");
		}
		else  {
			System.out.println("Long Arrays are Not Equal");
		}
	}
}
false
Both the Long Arrays are Equal

compare Double Arrays

In this Java program, we declared three double arrays with random elements. Then we will call the public static boolean equals (double[] d1, double[] d2) method to check whether they are equal or not.

package ArrayMethods;
import java.util.Arrays;

public class DoubleEquals {
	public static void main(String[] args) {
		double[] dblArray = {218.456, 120.654, 98.4148, 658.945, 315.458}; 
		double[] doubleArray = {218.456, 120.654, 98.4148, 658.945, 315.458}; 
		double[] andoubleArray = {218.45, 102.65, 908.41, 688.94, 325.45};  
		
		//Checking for Equality
		System.out.println(Arrays.equals(dblArray, andoubleArray));
				
		if(Arrays.equals(dblArray, doubleArray)) {
			System.out.println("Both the Double Arrays are Equal");
		}
		else  {
			System.out.println("Double Arrays are Not Equal");
		}
	}
}
false
Both the Double Arrays are Equal

Java equals to compare Float Arrays

In this Java program, we declared three float arrays with random elements. Then we will call the public static boolean equals (float[] f1, float[] f2) method to check whether they are equal or not.

package ArrayMethods;
import java.util.Arrays;

public class FloatEquals {
	public static void main(String[] args) {
		float[] floatArray = {12.28f, 3.64f, 4.98f, 9.65f, 1.65f}; 
		float[] fltArray = {12.28f, 3.64f, 4.98f, 9.65f, 1.65f}; 
		float[] anfloatArray = {2.98f, 6.45f, 8.65f, 1.55f, 2.5f}; 
		
		//Checking for Equality
		System.out.println(Arrays.equals(floatArray, fltArray));
				
		if(Arrays.equals(floatArray, anfloatArray)) {
			System.out.println("Both the Float Arrays are Equal");
		}
		else  {
			System.out.println("Float Arrays are Not Equal");
		}
	}
}
true
Float Arrays are Not Equal

Java equals to compare Char Arrays

In this Java program calls the public static boolean equals (char[] c1, char[] c2) to check whether they are equal.

package ArrayMethods;
import java.util.Arrays;

public class CharEquals {
	public static void main(String[] args) {
		char[] CharArray = {'g', 'a', 't', 'e'}; 
		char[] CharacterArray = {'g', 'a', 't', 'e', 'w', 'a', 'y'};
		char[] Character = {'g', 'a', 't', 'e', 'w', 'a', 'y'};
		
		//Checking for Equality
		System.out.println(Arrays.equals(CharArray, CharacterArray));
				
		if(Arrays.equals(CharacterArray, Character)) {
			System.out.println("Both are Equal");
		}
		else  {
			System.out.println("Not Equal");
		}
	}
}
false
Both are Equal

Compare Objects Arrays

In this Java program, Javac calls the public static boolean equals (Object[] Obj1, Object[] Obj2) to check whether they are equal.

package ArrayMethods;
import java.util.Arrays;

public class ObjectEquals {
	public static void main(String[] args) {
		String[] strArray = {"Orange", "Strawberry","Grape", "Banana", "Apple", "Kiwi"};
		String[] stringArray = {"India", "UK", "USA", "Australia", "Japan","China", "Korea"};
		String[] strngArray = {"India", "UK", "USA", "Australia", "Japan","China", "Korea"};
		//Checking for Equality
		System.out.println(Arrays.equals(strArray, stringArray));
				
		if(Arrays.equals(stringArray, strngArray)) {
			System.out.println("Both are Equal");
		}
		else  {
			System.out.println("Not Equal");
		}
	}
}
false
Both are Equal