I’m working on a programming lanuage, i need to convert an object (like var in javascript) in to the type of variable it should be. Ex:
if(object == variabletypes.string)
{
//convert object to string
}
else if(object ==variabletypes.int)
{
//convert to integer
}
Thanks for your time, any help would be appreciated.
Assuming you’ve got a
java.lang.Object, this is a start:The “conversion” here is primarily casting, operating under the assumption that the object already has the correct runtime type, and that you don’t need to actually alter the internal representation — for instance, by changing a
Stringto anintwithInteger#parseInt().Other potentially useful methods (since the question isn’t exactly clear) may include:
Object#getClass()Class#cast(Object)Class#isAssignableFrom(Class)