Skip to Content

Compare Two Java boolean Arrays Example

  1. /*
  2.   Compare Two Java boolean Arrays Example
  3.   This java example shows how to compare two boolean arrays for equality using
  4.   Arrays.equals method.
  5. */
  6.  
  7. import java.util.Arrays;
  8.  
  9. public class CompareBooleanArraysExample {
  10.  
  11.   public static void main(String[] args) {
  12.     //create boolean arrays
  13.     boolean[] blnArray1 = new boolean[]{true,false,true};
  14.     boolean[] blnArray2 = new boolean[]{true,false,true};
  15.    
  16.     /*
  17.       To compare two boolean arrays use,
  18.       static boolean equals(boolean array1[], boolean array2[])
  19.       method of Arrays class.
  20.      
  21.       It returns true if both arrays are equal. Arrays are considered as equal
  22.       if they contain same elements in same order.
  23.     */
  24.    
  25.     boolean blnResult = Arrays.equals(blnArray1,blnArray2);
  26.     System.out.println("Are two boolean arrays equal ? : " + blnResult);
  27.    
  28.     /*
  29.       Please note that two boolean array references pointing to null are
  30.       considered as equal.
  31.     */
  32.    
  33.   }
  34. }
  35.  
  36. /*
  37. Output of the program would be
  38. Are two boolean arrays equal ? : true
  39. */