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
Updated on: 2020-06-26T12:21:12+05:30

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements
close