How to compare two character arrays in Java

  1. char[] charArray1 = new char[]{'d','h','r','f'};
  2.     char[] charArray2 = new char[]{'d','h','r','f'};
  4.     /*
  5.       To compare two char arrays use,
  6.       static boolean equals(char array1[], char array2[]) method of Arrays class.
  8.       It returns true if both arrays are equal. Arrays are considered as equal
  9.       if they contain same elements in same order.
  10.     */
  12.     boolean blnResult = Arrays.equals(charArray1,charArray2);
    Useful while comparing the received password with the stored one. Since password should not be stored in String (String is immutable, So the password will be there in RAM as a non reference value, which is easy to access). So its better approach to use the char array.