Java Convert Object to String

In Java, you can convert an object to a string using the toString() method, which is defined in the Object class, and is inherited by all Java classes.

If you want to convert an object of a specific class to a string, you can override the toString() method in that class to provide a custom implementation that returns a string representation of the object. Here’s an example:

public class MyClass {
    private int id;
    private String name;

    public MyClass(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "MyClass{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

// Example usage:
MyClass obj = new MyClass(1, "John");
String str = obj.toString();
System.out.println(str); // prints "MyClass{id=1, name='John'}"

Alternatively, if you want a more general-purpose way to convert an object to a string, you can use the String.valueOf() method, which accepts an Object argument and returns its string representation. For example:

Object obj = new Integer(42);
String str = String.valueOf(obj);
System.out.println(str); // prints "42"

Java Object to String Example: Converting User-defined class:

Here’s an example of how to convert an object of a user-defined class to a string in Java using the toString() method:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

// Example usage:
Person p = new Person("John Doe", 30);
String str = p.toString();
System.out.println(str); // prints "Person{name='John Doe', age=30}"

In this example, the toString() method is overridden in the Person class to return a string representation of the Person object, which includes the person’s name and age. To convert an object of the Person class to a string, you simply call the toString() method on the object.

Java Object to String Example: Converting StringBuilder

Here’s an example of how to convert a StringBuilder object to a string in Java:

StringBuilder sb = new StringBuilder("Hello, world!");
String str = sb.toString();
System.out.println(str); // prints "Hello, world!"

In this example, the toString() method is called on the StringBuilder object sb to convert it to a String. The resulting String is then printed to the console.

StringBuilder is a mutable class in Java that represents a sequence of characters. It provides various methods to modify the sequence of characters, such as append(), insert(), and delete(). To convert a StringBuilder object to a String, you simply call the toString() method on the object. The toString() method returns a new String object that contains the same characters as the StringBuilder object.