Class getComponentType() method in Java with Examples (original) (raw)
Last Updated : 27 Jan, 2022
The getComponentType() method of java.lang.Class class is used to get the Class representing the component type of an array, if this class represents one. Else it returns null.
Syntax:
public Class getComponentType()
Parameter: This method does not accept any parameter.
Return Value: This method returns Class representing the component type of an array if this class represents one. Else it returns null.
Below programs demonstrate the getComponentType() method.
Example 1:
Java
import
java.util.*;
public
class
Test {
`` public
static
void
main(String[] args)
`` throws
ClassNotFoundException
`` {
`` Class myClass = Class.forName("Test");
`` System.out.println("Class represented by myClass: "
`` + myClass.toString());
`` System.out.println("ComponentType of myClass: "
`` + myClass.getComponentType());
`` }
}
Output:
Class represented by myClass: class Test ComponentType of myClass: null
Example 2:
Java
import
java.util.*;
public
class
Test {
`` public
static
void
main(String[] args)
`` throws
ClassNotFoundException
`` {
`` int
[] Arr =
new
int
[
5
];
`` Class arrClass = Arr.getClass();
`` System.out.println("ComponentType of myClass: "
`` + arrClass.getComponentType());
`` }
}
Output:
ComponentType of myClass: int
Reference: https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#getComponentType–
Similar Reads
- Class getTypeName() method in Java with Examples The getTypeName() method of java.lang.Class class is used to get the type name of this class, which provides the information about this class' type. The method returns the type name of this class in the form of String. Syntax: public String getTypeName() Parameter: This method does not accept any pa 2 min read
- Class getSimpleName() method in Java with Examples The getSimpleName() method of java.lang.Class class is used to get the simple name of this class, as given in the sourcecode. The method returns the simple name of this class in the form of String. If this class is anonymous, then this method returns empty string.Syntax: public String getSimpleName( 2 min read
- Class getConstructor() method in Java with Examples The getConstructor() method of java.lang.Class class is used to get the specified constructor of this class with the specified parameter type, which is the constructor that is public and its members. The method returns the specified constructor of this class in the form of Constructor object. Syntax 2 min read
- Class getModule() method in Java with Examples The getModule() method of java.lang.Class class is used to get the module of this entity. This entity can be a class, an array, an interface, etc. The method returns the module of the entity.Syntax: public Module getModule() Parameter: This method does not accept any parameter.Return Value: This met 2 min read
- Class getMethod() method in Java with Examples The getMethod() method of java.lang.Class class is used to get the specified method of this class with the specified parameter type, which is the method that is public and its members. The method returns the specified method of this class in the form of Method objects. Syntax: public Method getMetho 2 min read
- Class getName() method in Java with Examples The getName() method of java.lang.Class class is used to get the name of this entity. This entity can be a class, an array, an interface, etc. The method returns the name of the entity as a String.Syntax: public String getName() Parameter: This method does not accept any parameter.Return Value: This 1 min read
- Class getConstructors() method in Java with Examples The getConstructors() method of java.lang.Class class is used to get the constructors of this class, which are the constructors that are public. The method returns the constructors of this class in the form of array of Constructor objects. Syntax: public Constructor[] getConstructors() Parameter: Th 2 min read
- Class getAnnotationsByType() method in Java with Examples The getAnnotationsByType() method of java.lang.Class class is used to get the annotations of the specified annotation type present in this class. The method returns an array of annotations for the specified annotation type. Syntax: public A[] getAnnotationsByType(Class annotationClass) Para 2 min read
- Class getAnnotation() method in Java with Examples The getAnnotation() method of java.lang.Class class is used to get the annotation of the specified annotation type, if such an annotation is present in this class. The method returns that class in the form of an object. Syntax: public T getAnnotation(Class annotationClass) Parameter: This m 2 min read
- Class getModifiers() method in Java with Examples The getModifiers() method of java.lang.Class class is used to get the Java language modifiers of this class. The method returns an integer representing the encoded modifiers of this class. Syntax: public int getModifiers() Parameter: This method does not accept any parameter.Return Value: This metho 2 min read