×

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

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

Array Slicing in Java

Array slicing is a method in Java for obtaining a subarray of a specified array. Assume a[] is an array. It contains eight items indexed from a[0] to a[7].a[] =...

3 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

How to download and install Eclipse in Windows?

Download and Install Eclipse on Windows Eclipse is an open source IDE (Integrated Development Environment) which is used to help the programmers to provide a platform to write and run the...

1 minute read.

Character Array in Java

A character array is an array which holds values of character data types. It is different from a string array. The character array, string, and StringBuffer classes in the Java...

4 minutes read.

Dining Philosophers problem in Java

The Problem of the Dining Philosophers illustrates a concurrency issue involving the distribution of scarce resources among conflicting processes. Problem Statement Imagine a dining table with a circle in the middle and five...

3 minutes read.

Modules in Golang

Modules are a way to manage dependency versions and enable reproducible builds of Go programs. They were introduced in Go 1.11 and are now the recommended way to manage dependencies...

4 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.