This manual will illustrate the ways to convert an object to a String in Java.
How to Convert Object to String in Java?
In Java, there are two methods to convert an object to String as follows:
Note: These two methods are used for the objects of both predefined and user-defined classes. In this method, we will use the first method to convert a predefined class object and the other for the user-defined class object conversion to String.
Let’s start to see the working of these methods with examples.
Method 1: Convert Object of a User-Defined Class to String Using toString() Method
To convert an object to a String, you can use the “toString()” method of the “Object” class. In this section, we will use an object of the user-defined class by creating a class and then converting its object to a String.
Syntax
Follow the given syntax of the toString() method:
Here, “obj” is the object of a class that will be converted into a string using the “toString()” method.
Example
In this example, we will create a class named “User” that contains a String type variable “Name” and a parameterized constructor:
Then, in the main() method of another class named “Example”, we will first create an object “user” of the “User” class and pass a name as a parameter. Next, we will create a String type variable that stores the value after converting the object to a string using the toString() method. Lastly, we will print the variable that shows the object value as a string value:
Here, the reference id of the “user” object is displayed as a String value:
To verify if the object is successfully converted to a string, check its class type:
Output
Let’s check how to convert a predefined class object to a string in Java.
Method 2: Convert Object of Predefined Class to String Using valueOf() Method
There is another method in Java to convert an object of a class to a string called the “valueOf()” method. It belongs to the “String” class and is a static method. In this section, we will convert an object of a predefined Java class to a string
Syntax
Use the below-given syntax for the valueOf() method to convert an object to a string:
It takes the object “obj” as an argument.
Example
Here, we have an object “oStr” of the Java “Object” class having the following value:
Print the value stored in an object on the console:
Now, we will convert the object into a string using the “String.valueOf()” method, pass the object “oStr” as an argument, and store it in a String type variable, “objToStr”.
Finally, print the variable “objToStr” that stores a value of the object as a string:
The output shows that the valueOf() method successfully converted an object to a string:
We have provided all the procedures for converting an object to a string in Java.
Conclusion
For converting an object to a string, you can use the toString() method of the Java Object class and the valueOf() method of the String class. These methods are used to convert both predefined and user-defined class objects to the string. In this manual, we illustrated the procedures of the conversion of an object to a string in Java with detailed examples.