How can I convert a String to Object? Actually, I want to set
clientSession.setAttribute("username", "abc")
However, it shows
java.lang.String given, required java.lang.Object.
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. 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.
We can also convert the string to an object using the Class. forName() method. Parameter: This method accepts the parameter className which is the Class for which its instance is required.
The JavaScriptSerializer. Deserialize() method converts the specified JSON string to the type of the specified generic parameter object. The following example shows how to parse JSON string using JavaScriptSerializer. Deserialize() method.
A Java String
is an Object
. (String
extends Object
.)
So you can get an Object
reference via assignment/initialisation:
String a = "abc";
Object b = a;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With