Java

How to Convert Object to String in Java

Java allows you to convert any object of user-defined classes, or predefined classes, to a String. To do so, it is necessary to convert the first byte into an object byte, which can be handled easily to convert to strings. For this purpose, use the “toString()” method of the Java Object class, which is the basic method of all the Java classes.

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:

obj.toString();

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:

classUser{
    String Name;
    User(String name){
    this.Name=name;
    }
}

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:

publicclassExample {
    publicstaticvoidmain(String[] args) {
         User user = new User("John");
         String s= user.toString();
         System.out.println("String value: "+s);
    }
}

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:

User user = new User("John");

System.out.println("The object 'user' belongs to " + user.getClass());

String s= user.toString();

System.out.println("The string 's' belongs to " + s.getClass());

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:

String.valueOf(obj)

It takes the object “obj” as an argument.

Example

Here, we have an object “oStr” of the Java “Object” class having the following value:

Object oStr = "LinuxHint";

Print the value stored in an object on the console:

System.out.println("The Object value: "+oStr);

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”.

String objToStr = String.valueOf(oStr);

Finally, print the variable “objToStr” that stores a value of the object as a string:

System.out.println("Converted value as String: "+objToStr);

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.