In this guide, we will learn how to convert String to Object in Java. Object is a parent class of all the classes in Java so you can simply assign a string to an object to convert it.
Program to Convert String to Object using simple assignment
You can use the assignment operator (=) to simply assign the string to object. Since Object is a parent class of String, it can store the string value.
class JavaExample {
public static void main(String[] args)
{
String str = "BeginnersBook";
Object obj = str; //assigning string to object
//the class of the value stored in the Object obj
System.out.println("Class of the value stored in obj: "
+obj.getClass().getName());
System.out.println("The value contained in obj is : "+obj);
}
}
Output: