×

How to Convert Object to String in Java

How to Convert Object to String in Java

You can convert any Object to String in Java whether it is a user-defined class, StringBuilder or StringBuffer, etc. There are two methods to convert Object to String.

  • Using String.valueOf(object) method
  • Using toString() method

Example

In this example, we are going to convert the object of Student class into String using both the above methods. Here we have created a body of Student class. Inside the main() method,  we have created an object of Student class. The toString() method converts the Object into String and the string stores into variable s. Another method String.valueOf() is used to convert Object to String.  String.valueOf(), method parses the object of Student class as an argument and converts it into the String. The converted string stored in the variable s1. The first println statement prints the string s converted by toString() method. The second println statement prints the string s1 converted by String.valueOf() method.

class Student{}
public class ObjectToStringExample
{
public static void main(String args[])
{
Student stud= new Student();   //object of Student class
String s=stud.toString(); //converts object to String using toString() method
String s1=String.valueOf(stud);             //converts object to String using String.valueOf() method
System.out.println(s);
System.out.println(s1);
}
}

Output

Student@15db9742
Student@15db9742

We get the reference id of the Student class as output.

Converting StringBuilder Object into String

StringBuilder objects are similar to String objects, except that they can be modified. It also used to create mutable string objects. StringBuilder is same as StringBuffer, but there is a slight difference. StringBuffer is synchronized while StringBuilder is not synchronized.

Example

In this example, we are going to convert StringBuilder Object into String.  Here we have taken variable str of type String and initialized a string “Delhi” to it. In the next statement, we have created an object of StringBuilder class and parse a string into it as an argument. sb is an object of StringBuilder class. reverse() is the method that reverses the characters within a StringBuilder object. The toString() method converts the Object into String and stores into the variable rev. The first println statement prints the string which we have initialized. The second println statement prints the reverse string of the initialized string.

public class ObjectToStringExample1
{
public static void main(String args[])
{
String str="Delhi";
StringBuilder sb= new StringBuilder(str);
sb.reverse();   //reverse the string
String rev=sb.toString(); //converting StringBuilder Object into String
System.out.println("The string is: "+str);
System.out.println("Reverse string is: "+rev);
}
}

Output

The string is: Delhi
Reverse String is: ihleD

Related Topics

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

3 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

Interfaces and Classes in Strings in Java

CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration...

4 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Advantages of Generics in Java

Generic offers a variety of benefits. The programmer's life is made easier by using generic Java. In this section, we are going to discuss about Java's generic’s and its benefits. 1....

4 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.