BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String to Object Conversion

Last Updated: November 8, 2022 by Chaitanya Singh | Filed Under: java

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: