Home » Java programming language

Java Class class getComponentType() method with example

Class class getComponentType() method: Here, we are going to learn about the getComponentType() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 04, 2019

Class class getComponentType() method

  • getComponentType() method is available in java.lang package.
  • getComponentType() method is used to returns the Class denoting the component type of an array.
  • getComponentType() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getComponentType() method does not throw an exception at the time of returning Class.

Syntax:

    public Class getComponentType();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Class, it returns the component type in terms of "Class" type.

Example:

// Java program to demonstrate the example 
// of Class getComponentType() method of Class

public class GetComponentTypeOfClass {
    public static void main(String[] args) {
        // Creating an array of Integer type
        Integer[] num = new Integer[] {
            10,
            20,
            30
        };

        // It returnd the Class denoting the component type
        Class cl = num.getClass();

        // By using getComponentType() method is used to
        // get the component of the given Class
        Class comp_type = cl.getComponentType();

        // If any component exists
        if (comp_type != null)
            System.out.println("Component Type: " + comp_type);
        else
            System.out.println("No Component Type Associated");

    }
}

Output

Component Type: class java.lang.Integer


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.