In Java, would it be possible to implement a function that would take an object as input, and then convert the object to a type that is specified as a parameter?
I'm trying to implement a general-purpose type conversion function for primitive data types, but I don't know exactly where to start:
public static Object convertPrimitiveTypes(Object objectToConvert, String typeToConvertTo){
//Given an object that is a member of a primitive type, convert the object to TypeToConvertTo, and return the converted object
}
For example, convertPrimitiveTypes(true, "String") would return the string "true", and convertPrimitiveTypes("10", "int") would return the integer 10. If the conversion were not well-defined (for example, converting a boolean to an integer), then the method would need to throw an exception, and terminate the program.