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

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:

Java String to Object Example Output

Getting String instance of the Object class

We can use the forName() method of Class to get an instance of Class, which represents a String. The syntax of forName() method is:

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

You can pass the desired class name to this method and it returns an instance of Class which represents object of the passed class. Since Class is a subclass of Object, when we use the getSuperclass() method of this class on the obtained instance, it returns Object class.

class JavaExample {
  public static void main(String[] args) throws Exception
  {
    // By passing the String class inside the forName() method
    // we can get an instance of Class that represents a String
    Class obj = Class.forName("java.lang.String");

    // We can verify the class of the obtained instance
    System.out.println("Class name of obj: " + obj.getName());

    // The superclass of obj should return Object class
    System.out.println("Super class of obj: "+obj.getSuperclass().getName());
  }
}

Output:

Get a String instance of Object class

Recommended Posts

  • Java String to boolean Conversion
  • Java String to long Conversion
  • Java String to double Conversion
  • Java String to int Conversion
  • Java String to char Conversion
  • Java String to Date Conversion

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap