Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a String to Object [closed]

Tags:

java

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.
like image 982
Arpssss Avatar asked Jan 22 '12 17:01

Arpssss


People also ask

How do you turn a string into an 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.

Can we convert string to object in java?

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.

Can we convert string to object C#?

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.


1 Answers

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;
like image 153
Oliver Charlesworth Avatar answered Oct 14 '22 22:10

Oliver Charlesworth