Java Program to Check Whether the Specified Class Object Represents an Array or Not

You are currently viewing Java Program to Check Whether the Specified Class Object Represents an Array or Not

Java program to check whether the specified Class object represents an array or not

// Java program to check the specified Class object 
// represents an array

public class Main {
  public static void main(String[] args) throws ClassNotFoundException {
    int arr[] = new int[5];

    Class cls1 = arr.getClass();
    Class cls2 = float.class;

    boolean res1 = cls1.isArray();
    boolean res2 = cls2.isArray();

    System.out.println("Is " + cls1 + " an array : " + res1);
    System.out.println("Is " + cls2 + " an array : " + res2);
  }
}

Output

Is class [I an array : true
Is float an array : false

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply