How to Convert String to Object in Java?


In Java, Object is a parent class of all the classes. So, we can directly assign a string to an object as each class is internally a child class of the Object class.

A string is generally considered a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding.

The object data type is also known as non-primitive or reference data type; these are not predefined ones; instead, we need to create them.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

  • Suppose the given String is “Hey there”. Then it will be converted to object “ob1”

Instance-2

  • Suppose the given String is “java.lang.String". Then it will be converted to object “c”

Instance-3

  • Suppose the given String is “Hello World". Then it will be converted to object “ob2”

Algorithm

Algorithm - 1 (String to Object)

  • Step-1 − Declare the String.

  • Step-2 − Assign string to an object.

  • Step-3 − Print the result.

Algorithm - 2 (String to Class Object)

  • Step-1 − Create the object of the class using forName() method.

  • Step-2 − Get the name of the class.

  • Step-3 − Print the result.

Syntax

forName() − It is used to get the instance of this Class with the specified class name. It belongs to java.lang.Class pacakage.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Assignment Operator

  • By Using Class.forName() Method

Let’s see the program along with its output one by one.

Approach-1: By Using Assignment Operator

In this approach, String is converted object by using assignment operator. An assignment operator assigns the string into the reference variable of the Object class.

Example

public class Main {
   public static void main(String args[] ){
		
      // Declare the string
      String str = "Hey there";
		
      // assigning string to an object
      Object ob = str;
	   
      //printing the result i.e by converting the string to object
      System.out.println("Object after conversion from String : "+ ob);
   }
}

Output

Object after conversion from String : Hey there

Approach-2: By Using Class.forName() Method

In this approach, java.lang package creates an instance of java.lang.Class and forces the class loader to load this class, and execute code in its static block.

Example

public class Main {
   public static void main(String args[] ) throws ClassNotFoundException{
		
      //creating the object of the class using forName method 
      Class c = Class.forName("java.lang.String");

      // getting the name of the class and printing the result
      System.out.println("class name: " + c.getName());
   }
}

Output

class name: java.lang.String

In this article, we explored different approaches to convert String to Object by using Java programming language.

Updated on: 04-May-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements