Java.lang Package Classes

Java Class - toGenericString() Method



The java.lang.Class.toGenericString() method returns a string describing this Class, including information about modifiers and type parameters. The string is formatted as a list of type modifiers, if any, followed by the kind of type (empty string for primitive types and class, enum, interface, or @interface, as appropriate), followed by the type's name, followed by an angle-bracketed comma-separated list of the type's type parameters, if any. A space is used to separate modifiers from one another and to separate any modifiers from the kind of type. The modifiers occur in canonical order. If there are no type parameters, the type parameter list is elided.

Syntax

public String toGenericString()

Parameters

No parameter is required.

Return Value

Returns a string describing this Class, including information about modifiers and type parameters.

Exception

NA.

Example:

The example below shows the usage of java.lang.Class.toGenericString() method.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    MyClass x = new MyClass();
    Integer y = 5;

    //get class of the object and  
    //convert to GenericString for x
    Class xcls = x.getClass();
    System.out.println("xcls is: " + xcls.toGenericString());

    //get class of the object and  
    //convert to GenericString for y
    Class ycls = y.getClass();
    System.out.println("ycls is: " + ycls.toGenericString());
  }
}

The output of the above code will be:

xcls is: public class MyClass
ycls is: public final class java.lang.Integer

❮ Java.lang - Class