×

How to Convert String to Object in Java

How to Convert String to Object in Java

The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to convert String to Object.

  • Using Assignment Operator
  • Using Class.forName() method

Using Assignment Operator

You can convert String to Object by using the assignment operator. An assignment operator assigns the string into the reference variable of the Object class.

Example

In the following example, we have taken a variable str of type String and initialized “book” into it. An Object is a super class of all classes. obj is a reference variable of the class Object that stores the string str as an object. The println statement prints the object that contains a string.

public class StringToObjectExample
{  
public static void main(String args[])
{  
String str="book";  
Object obj=str;  
System.out.println(obj);  
}
}

Output

book

Using Class.forName() method

There is another method Class.forName() to convert String to Object. It belongs to java.lang package. It creates an instance of java.lang.Class and forces the class loader to load this class, and execute code in its static block. The Class.forName() method returns the Class object associated with the class or interfaces with the specified string.

Example

In the following example, getName() is the method of java.lang.reflect.Method class. It returns the method name as a string.

The java.lang.Class class has a method getSuperclass(). It is used to retrieve the direct super class. It returns a Class object representing the super class of the Class Object on which the method is called. If the method is called on the object class, then it returns null.

The first println statement prints the class name and the second println statement prints the super class name of the class java.lang.String.

We should use either try-catch block or write throws Exception just after the main method because it throws ClassNotFoundException.

public class StringToObjectExample2
{  
public static void main(String args[])throws Exception
{  
Class c=Class.forName("java.lang.String");  
System.out.println("class name: "+c.getName());  
System.out.println("super class name: "+c.getSuperclass().getName());  
}
}

Output

class name: java.lang.String

super class name: java.lang.Object

Related Topics

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

How to Convert int to double in Java

How to Convert int to double in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Comparetoignorecase in Java

In Java, the function compareToIgnoreCase ( ) is part of the String class, which is part of the java.lang package. It is used to compare any two strings while disregarding...

4 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.