java: how can i do dynamic casting of a variable from one type to another? -


I want to do dynamic casting for Java variables, the casting type is stored in a different variable.

This is a regular casting:

  string A = (string) 5;  

This is what I need:

  string theType = 'string'; String A = (TheType) 5;  

Is this possible? And if so how? Thanks!

Update

I'm trying to populate a square with a hash map.

This is the manufacturer:

  Public connect (hashmap> string, object & gt; obj) {for (entry)  string, object & gt; ; Entry: obj.entrySet ()) {try {field f = this.getClass (). GetField (entry keying ()); F.set (this, entry .getValue ()); / * And lieutenant; = Casting Problem * Hold (Noch Fielded Experience Pre) {Log. Terror ("field not found" "+ + entry.getKey () + '' '');} Hold (illegal entry) {Log. Panic (Here's the problem that some square variables are double types, and if Number 3 is received, then it looks as an integer and I have type problem.   

About your update, the only way to solve this in Java is to write code that is too many if and else and instanceof Expression Covering all the cases with the tricks. What do you try to do, as it is used for the program with dynamic static languages, whatever you try to do is almost impossible and possibly whatever you Try to do that, you have to choose a completely different approach. Stable languages ​​are not as flexible as dynamic people :)

The following are examples (ie object Conhwartr ) and down (ie adapter ).


This does not mean,

  string A = (theType) 5; The type of  

a is to be statically string , so it does not make sense to insert a dynamic for this static type.

PS: The first line of your example is class < String & gt; Can be written as. StringClass = String.class; But still, you can not use the stringClass to enter the variable.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -