Thursday, July 4, 2024
HomeLanguagesJavaJava Program to Convert String to Object

Java Program to Convert String to Object

In-built Object class is the parent class of all the classes i.e each class is internally a child class of the Object class. So we can directly assign a string to an object.

Basically, there are two methods to convert String to Object. Below is the conversion of string to object using both of the methods.

  1. Using Assignment Operator
  2. Using Class.forName() method

Method 1: Using the Assignment Operator

An assignment operator assigns string into reference variable of the object class.

Java




// Java Program to convert string to an object
import java.io.*;
import java.util.*;
   
class GFG {
    public static void main(String[] args)
    {
        // string
        String s = "GeeksForGeeks";
        
        // assigning string to an object
        Object object = s;
        
        // to check the data-typeof the object 
        // to confirm that s has been stored in object
       System.out.println("Datatype of the variable in object is : "
                          +object.getClass().getName());
        
       System.out.println("object is : "+object);
    }
}


Output

Datatype of the variable in object is : java.lang.String
object is : GeeksForGeeks

Method 2 : Using Class.forName() method

We can also convert the string to an object using the Class.forName() method.

Syntax:

public static Class<T> forName(String className) throws ClassNotFoundException

Parameter: This method accepts the parameter className which is the Class for which its instance is required.

Return Value: This method returns the instance of this Class with the specified class name.

  • Class class belongs to the java.lang package.
  • The java.lang.Class class has a method getSuperclass(). It is used to retrieve the superclass of the current class. This method returns a Class object which represents the superclass of the Class Object on which the method is called. If the method is called on the object class, then it will return null, since the Object class is the topmost class in the class hierarchy and there cannot be any superclass of the Object class.

Java




// Java program to convert the string to an object
  
class GFG {
    public static void main(String[] args) throws Exception
    {
        // getting the instance of the class passed in
        // forName method as a string
        Class c = Class.forName("java.lang.String");
        
        // getting the name of the class
        System.out.println("class name: " + c.getName());
        
        // getting the name of the super class
        System.out.println("super class name: "
                           + c.getSuperclass().getName());
    }
}


Output

class name: java.lang.String
super class name: java.lang.Object

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments