Java Class.getComponentType()

Syntax

Class.getComponentType() has the following syntax.

public Class <?> getComponentType()

Example

In the following code shows how to use Class.getComponentType() method.


//  ww  w.  j a v a  2  s  . c o m
public class Main {

  public static void main(String[] args) {

    String[] arr = new String[] { "java2s.com" };

    // returns the Class representing the component type
    Class arrClass = arr.getClass();
    Class componentType = arrClass.getComponentType();
    if (componentType != null) {
      System.out.println("ComponentType = " + componentType.getName());
    } else {
      System.out.println("ComponentType is null");
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable