How to convert String to Object in Java

We can convert String to Object in java with the assignment operator. Each class is internally a child class of Object class. So you can assign a string to Object directly.

Example:

Input:  str="Hello"
Output: Hello

Approach

Java


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


No comments:

Post a Comment