Java Convert String to Object

In Java, you can convert a String to an Object in a couple of ways depending on what you want to achieve. Here are some examples:

  1. Using the valueOf() method:
String str = "123";
Object obj = Integer.valueOf(str); // convert String to Integer Object
  1. Using the constructor of the desired Object class:
String str = "Hello World";
Object obj = new String(str); // convert String to String Object

Note that in both examples above, the resulting Object is a new instance of the desired class that encapsulates the same value as the original String.

It’s important to note that in some cases, you might want to convert a String to a specific subclass of Object rather than just Object itself. In such cases, you can use the appropriate subclass constructor or static method instead of the generic valueOf() method. For example, to convert a String to a Boolean object, you can use the Boolean.valueOf() method:

String str = "true";
Boolean boolObj = Boolean.valueOf(str); // convert String to Boolean Object

Java String to Object Example:

Here’s an example of converting a String to an Object in Java:

public class StringToObjectExample {
    public static void main(String[] args) {
        // Convert a String to an Integer Object
        String intStr = "123";
        Integer intObj = Integer.valueOf(intStr);
        System.out.println("Integer Object: " + intObj);

        // Convert a String to a Double Object
        String doubleStr = "3.14";
        Double doubleObj = Double.valueOf(doubleStr);
        System.out.println("Double Object: " + doubleObj);

        // Convert a String to a Boolean Object
        String boolStr = "true";
        Boolean boolObj = Boolean.valueOf(boolStr);
        System.out.println("Boolean Object: " + boolObj);

        // Convert a String to a custom Object
        String customStr = "Custom Value";
        CustomObject customObj = new CustomObject(customStr);
        System.out.println("Custom Object: " + customObj);
    }

    static class CustomObject {
        private String value;

        public CustomObject(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return value;
        }
    }
}

In this example, we first convert a String containing an integer value to an Integer object using the Integer.valueOf() method. We then do the same for a double value and a boolean value using the Double.valueOf() and Boolean.valueOf() methods respectively.

Finally, we show how to convert a String to a custom object by creating a simple CustomObject class that takes a string value in its constructor and provides a toString() method to display the value. We create a new instance of CustomObject using the new keyword and passing the String value as an argument to the constructor.

Java String to Class object Example:

In Java, you can convert a String to a Class object using the Class.forName() method. Here’s an example:

public class StringToClassExample {
    public static void main(String[] args) {
        try {
            // Convert a String to a Class object
            String className = "java.util.Date";
            Class<?> clazz = Class.forName(className);
            System.out.println("Class: " + clazz);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

In this example, we first create a String variable containing the fully qualified name of the class we want to convert to a Class object. In this case, we’re converting the String "java.util.Date" to a Class object representing the Date class in the java.util package.

We then use the Class.forName() method to convert the String to a Class object. This method throws a ClassNotFoundException if the specified class can’t be found, so we catch that exception and print its stack trace if it occurs.

Finally, we print the Class object using the toString() method, which returns the fully qualified name of the class. In this case, the output would be:

Class: class java.util.Date

This shows that we have successfully converted the String "java.util.Date" to a Class object representing the Date class.