×

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

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Java Math floorDiv() Method

The floorDiv () method of Math class returns the largest integer value that is less than or equal to the algebraic quotient. It firstly divides the dividend and divisor and...

2 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

JDBC vs ODBC

Difference Between JDBC and ODBC ODBC: ODBC (Open Database Connectivity) is the accepted method for accessing databases among organisations and programmers. A database is linked to other programmes, such as word processors, spreadsheets,...

4 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Various operations on the Queue using Stack in Java

The Java Collections Framework's core data structures are the Stack and Queue. They are used to store and retrieve identical data in a presentation sequence. These two linear data structures...

7 minutes read.

Morris Traversal for Preorder in Java

Without the use of recursion or stacks, we traverse a tree using the Morris algorithm. The linked binary tree is the foundation of the Morris traversal. Preorder Morris Traversal Algorithm The preorder...

3 minutes read.