English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Program to Compare Two Java Short Arrays

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

Let's first declare and initialize some short arrays.

short[] arr1 = new short[] { 20, 15, 35, 55, 69 });
short[] arr2 = new short[] { 20, 15, 35, 55, 69 });
short[] arr3 = new short[] { 22, 19, 30, 45, 78 });

Now let's compare the two arrays above.

Arrays.equals(arr1, arr2));

Similarly, use it for other arrays and compare

Example

import java.util.*;
public class Demo {
   public static void main(String []args) {
      short[] arr1 = new short[] { 20, 15, 35, 55, 69 });
      short[] arr2 = new short[] { 20, 15, 35, 55, 69 });
      short[] arr3 = new short[] { 22, 19, 30, 45, 78 });
      System.out.println(Arrays.equals(arr1, arr2));
      System.out.println(Arrays.equals(arr2, arr3));
      System.out.println(Arrays.equals(arr1, arr3));
   }
}

Output Result

true
false
false