Java Program to compare two Java char Arrays

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 13-Nov-2018 10:32:04
Advertisements