Class toGenericString() method in Java with Examples

The toGenericString() method of java.lang.Class class is used to convert the instance of this Class to a string representation along with the information about the modifiers and type parameters. This method returns the formed string representation.

Syntax:

public String toGenericString()

Parameter: This method does not accept any parameter.

Return Value: This method returns the String representation of this object.

Below programs demonstrate the toGenericString() method.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate toGenericString() method
  
public class Test {
    public static void main(String[] args)
        throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.String");
  
        System.out.print("Class represented by c1: ");
  
        // toGenericString method on c1
        System.out.println(c1.toGenericString());
    }
}

chevron_right


Output:

Class represented by c1: public final class java.lang.String

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate toGenericString() method
  
public class Test {
    public static void main(String[] args)
        throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c2 = int.class;
        Class c3 = void.class;
  
        System.out.print("Class represented by c2: ");
  
        // toGenericString method on c2
        System.out.println(c2.toGenericString());
  
        System.out.print("Class represented by c3: ");
  
        // toGenericString method on c3
        System.out.println(c3.toGenericString());
    }
}

chevron_right


Output:

Class represented by c2: int
Class represented by c3: void

Reference: https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#toGenericString–

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.