Java Class - toString() Method
The java.lang.Class.toString() method is used to convert the object to a string. The string representation is the string "class" or "interface", followed by a space, and then by the fully qualified name of the class in the format returned by getName.
Syntax
public String toString()
Parameters
No parameter is required.
Return Value
Returns a string representation of this class object.
Exception
NA.
Example:
In the example below, the java.lang.Class.toString() method returns the name of the given class.
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 string for x Class xcls = x.getClass(); System.out.println("Class = " + xcls.toString()); //get class of the object and //convert to string for y Class ycls = y.getClass(); System.out.println("Class = " + ycls.toString()); } }
The output of the above code will be:
Class = class MyClass Class = class java.lang.Integer
❮ Java.lang - Class