Q:

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

belongs to collection: Java Class and Object Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will check whether an object represents an array or not using the isArray() method.

Program/Source Code:

The source code to check whether the specified Class object represents an array or not is given below. The given program is compiled and executed successfully.

// 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

Explanation:

In the above program, we created a public class Main that contains a main() method. The main() method is the entry point for the program. Here, we checked whether an object represents an array or not using the isArray() method. The isArray() method returns true if the specified object represents an array otherwise it returns false.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to check whether the specified Class ... >>
<< Java program to check anonymous class using isAnon...