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

We can convert an object to String in Java whether it is user-defined class, StringBuilder, StringBuffer or anything else.
There are the following ways to convert object to string in Java:
| Method | Description |
|---|---|
| Object.toString() | Provides a textual representation of the object, detailing its characteristics or state. |
| String.valueOf() | A static function of the String class that transforms an object into its string form. |
| + Operator | It facilitates combining any data type with a string, creating a new string that includes the added content. |
| String.join() | Merges an array of elements into a single string, utilizing a specified separator between each element's string form. |
The toString() method in Java enables the conversion of user-defined class objects into descriptive strings, that aids in debugging and enhances code readability.
Filename: ToStringMethod.java
Output:
Student{name='Alice', age=20}
The valueOf() method in Java is a String class static method. It converts different types of values, including objects and Strings. When working with objects of predefined classes such as StringBuilder, the valueOf() method can convert these objects into strings. Let's understand it with an example.
Filename: ValueOfMethod.java
Output:
String representation: TeamMember [firstName=Jainey, lastName=Jain] TeamMember's full name: Jainey Jain
In Java, the + Operator is overloaded as a concatenation operator for strings. When used with strings and other data types, including objects, the + Operator implicitly converts the non-string elements to strings. The conversion often involves calling the toString() Method of the concatenated object.
Filename: ObjToString.java
Output:
The details of the book are: Book{title='1984', author='George Orwell'}
The join() method in Java is a static method of the String class used to concatenate the elements of an array or iterable into a single String, with a specified delimiter between each element. While the primary use of join() is for concatenating strings. It can also indirectly convert an object to a string, given that the object's toString() Method is effectively utilized.
Filename: JoinMethod.java
Output:
Person{name='Alice', age=30}, Person{name='Bob', age=25}, Person{name='Charlie', age=35}
Let's see the simple code to convert String to Object in Java.
ObjectToStringExample.java
Output:
Emp@2a139a55 Emp@2a139a55
In the above Java program, we observe that a reference id of Emp class is printed on the console.
Let's see the simple code to convert StringBuilder object to String in Java.
ObjectToStringExample2.java
Output:
String is: hello Reverse String is: olleh
Now we can write the code to check the palindrome string.
ObjectToStringExample3.java
Output:
Palindrome String
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India