Java Convert Object to String

We can convert Object to String in java using toString() method of Object class or String.valueOf(object) method.

Java Convert Object to String

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.

Test it Now

Output:

Emp@2a139a55
Emp@2a139a55

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.

Test it Now

Output:

String is: hello
Reverse String is: olleh

Now you can write the code to check the palindrome string.

Test it Now

Output:

Palindrome String

So, you can convert any Object to String in java using toString() or String.valueOf(object) methods.

Next TopicJava int to long




Contact US

Email:[email protected]

Java Object to String
10/30