Java Convert Object to String
We can convert Object to String in java using toString() method of Object class or String.valueOf(object) method.
You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else.
Here, we are going to see two examples of converting Object into String. In the first example, we are going to convert Emp class object into String which is an user-defined class. In second example, we are going to convert StringBuilder to String.
Java Object to String Example: Converting User-defined class
Let’s see the simple code to convert String to Object in java.
Output:
As you can see above, a reference id of Emp class is printed on the console.
Java Object to String Example: Converting StringBuilder
Let's see the simple code to convert StringBuilder object to String in java.
Output:
String is: hello Reverse String is: olleh
Now you can write the code to check the palindrome string.
Output:
Palindrome String
So, you can convert any Object to String in java using toString() or String.valueOf(object) methods.