Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
array
type-check
issametype
communitycreator

What is ArrayUtils.isSameType in Java?

abhilash

isSameType is a static method of the ArrayUtils class that checks whether two arrays are of the same type, considering multi-dimensional arrays.

Add apache commons-lang package

ArrayUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

Importing ArrayUtils

You can import the ArrayUtils class as follows:


import org.apache.commons.lang3.ArrayUtils;

Syntax

public static boolean isSameType(final Object array1, final Object array2)

Parameters

  • final Object array1: first array

  • final Object array2: second array

Returns

This method returns true if the two arrays are of same type, and returns false otherwise.

Code

Example 1

  • array1 = new int[]{1,2,3,4,5}
  • array2 = new int[]{1,2,3}

Applying the isSameType function will result in true, as the two arrays are of the same type.

Example 2

  • array1 = new int[]{1,2,3,4,5}
  • array2 = new char[]{'a','b','v'}

Applying the isSameType function will result in false, as the two arrays are of different types.

import org.apache.commons.lang3.ArrayUtils;

public class Main {

    public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,2,3,2,2,4};
        char[] array2 = new char[]{'a', 's', 'f'};
        boolean check = ArrayUtils.isSameType(array, array2);
        System.out.println("int array and boolean array2 are of same type - " + check);
        check = ArrayUtils.isSameType(array, array);
        System.out.println("int array and int array are of same type - " + check);
    }
}

Output


int array and boolean array2 are of same type - false
int array and int array are of same type - true

RELATED TAGS

java
array
type-check
issametype
communitycreator
RELATED COURSES

View all Courses

Keep Exploring