How to Convert Object to String in Java

We are going to start with the Conversion of Object to String in Java with its approach and programming, before that let us look into both String and Object and what do they mean?

In computer programming, a string is a sequence of characters that is used either as a constant or as a variable. The latter may allow for the mutation of its elements and the modification of their length. It may be fixed (after creation). In computing, a string is a data type that is frequently implemented as an array of bytes (or words) that stores a succession of items, most commonly characters, using some character encoding.

A Java object is a member of a Java class (also known as an instance). There is an identity, a behaviour, and a state for each item. Fields (variables) hold an object’s state, whereas methods (functions) exhibit the object’s action. Templates, often known as classes, are used to build objects at runtime.

 

What’s the Approach?

 

  • Create an object of a Different class, as here it is TechDecode obj=new TechDecode();

 

  • Convert it to string using obj.toString();

 

  • Print the String

 

Also Read: How to Convert a String to Date in Java

 

Java Program to Convert Object to String:

 

/*

 * TechDecode Tutorials

 *

 * How to Convert Object to String

 *

 */

class TechDecode{}




public class Object_To_String

{

    public static void main(String args[])

    {

        //object of Techdecode class

        TechDecode obj= new TechDecode();

        //converts object to String using toString() method

        String s=obj.toString();

        System.out.println("Object to String is: "+s);

    }

}

 

Output:

Object To String

Leave a Reply

Your email address will not be published. Required fields are marked *