Java Program to compare two Java char Arrays


To compare two Java char arrays, use the Arrays.equals() method.

Let us first declare and initialize some char arrays.

char[] arr1 = new char[] { 'p', 'q', 'r' };
char[] arr2 = new char[] { 'p', 'r', 's' };
char[] arr3 = new char[] { 'p', 'q', 'r' };

Now let us compare any two of the above arrays.

Arrays.equals(arr1, arr2));

In the same way, work it for other arrays and compare them.

The following is an example.

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String []args) {
      char[] arr1 = new char[] { 'p', 'q', 'r' };
      char[] arr2 = new char[] { 'p', 'r', 's' };
      char[] arr3 = new char[] { 'p', 'q', 'r' };
      System.out.println(Arrays.equals(arr1, arr2));
      System.out.println(Arrays.equals(arr2, arr3));
      System.out.println(Arrays.equals(arr1, arr3));
   }
}

Output

false
false
true

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements