English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java program to compare two boolean arrays

You need to import the following package to use this method-

import java.util.Arrays;

Use the Arrays.equals() method to compare two boolean arrays. Let's first create some arrays to compare.4We have

boolean[] myArr1 = new boolean[] { true, false, true, true, true };
boolean[] myArr2 = new boolean[] { true, false, true, true , true };
boolean[] myArr3 = new boolean[] { false, false, true, true, true };
boolean[] myArr4 = new boolean[] { true, false, false, true , false };

Now, let's compare boolean arrays1and2.

Arrays.equals(myArr1, myArr2);

Similarly, we can also compare other arrays.

Let's see a complete example of comparing boolean arrays in Java.

Example

import java.util.Arrays;
public class Demo {
   public static void main(String[] args) {
      boolean[] myArr1 = new boolean[] { true, false, true, true, true };
      boolean[] myArr2 = new boolean[] { true, false, true, true , true };
      boolean[] myArr3 = new boolean[] { false, false, true, true, true };
      boolean[] myArr4 = new boolean[] { true, false, false, true , false };
      System.out.println(Arrays.equals(myArr1, myArr2));
      System.out.println(Arrays.equals(myArr2, myArr3));
      System.out.println(Arrays.equals(myArr3, myArr4));
      System.out.println(Arrays.equals(myArr1, myArr4));
   }
}

Output Result

true
false
false
false