In the previous article we have discussed Java Program to Convert Timestamp to Date
In this article we will see how to convert Object to String.
Program to Convert Object to String
Let’s see different approaches one by one.
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Method-1: Java Program to Convert Object to String By Using toString() method
Let’s say obj
is the object of an User Defined class. We can convert this object to String by using toString()
method. Let’s see the program to know how it actually works.
Program:
//A user defined class //Student class created class Student { } //Driver class //Main class public class Main { public static void main(String args[]) { //object of Student class created Student obj=new Student(); //converting object to String by using toString() method //and assigning the converted string value to a string variable str String str=obj.toString(); //printing the string value System.out.println("Converted object to String : "+str); } }
Output: Converted object to String : Student@6a6824be
Method-2: Java Program to Convert Object to String By Using valueOf() method
Let’s say obj
is the object of an User Defined class. We can convert this object to String by using valueOf()
method. Let’s see the program to know how it actually works.
//A user defined class //Student class created class Student { } //Driver class //Main class public class Main { public static void main(String args[]) { //object of Student class created Student obj=new Student(); //converting object to String by using valueOf() method //and assigning the converted string value to a string variable str String str=String.valueOf(obj); //printing the string value System.out.println("Converted object to String : "+str); } }
Output: Converted object to String : Student@6a6824be
Method-3: Java Program to Convert Object to String Converting StringBuilder Object toString
In this method we have converted the object of StringBuilder class to String. Let’s see the program to know how it actually works.
str
is a string variable which holds astring value.sb
is the object of StringBuilder classoutput
is the string variable which holds the converted value(object to string)
//A user defined class //Student class created class Student { } //Driver class //Main class public class Main { public static void main(String args[]) { //String varible declared with assigned value as "BTechGeeks" String str="BtechGeeks"; //Object of StringBuilder class created 'sb' //And string 'str' passed to StringBuilder Object StringBuilder sb=new StringBuilder(str); //StringBuilder to String //object to string String output=sb.toString(); System.out.println("String is : "+str); System.out.println("Converted object to String : "+str); } }
Output: String is : BtechGeeks Converted object to String : BtechGeeks
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language
Related Java Program: