Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { /** * This method parses the given value into the specified primitive or wrapper class. * @param clazz - primitive or wrapper class used to parse * @param value - string to be parsed * @return object of type clazz parsed from the string * @author Trisan Bepler */ public static Object toObject(Class<?> clazz, String value) { if (Boolean.TYPE == clazz) return Boolean.parseBoolean(value); if (Byte.TYPE == clazz) return Byte.parseByte(value); if (Short.TYPE == clazz) return Short.parseShort(value); if (Integer.TYPE == clazz) return Integer.parseInt(value); if (Long.TYPE == clazz) return Long.parseLong(value); if (Float.TYPE == clazz) return Float.parseFloat(value); if (Double.TYPE == clazz) return Double.parseDouble(value); if (Boolean.class == clazz) return Boolean.parseBoolean(value); if (Byte.class == clazz) return Byte.parseByte(value); if (Short.class == clazz) return Short.parseShort(value); if (Integer.class == clazz) return Integer.parseInt(value); if (Long.class == clazz) return Long.parseLong(value); if (Float.class == clazz) return Float.parseFloat(value); if (Double.class == clazz) return Double.parseDouble(value); if (Character.class == clazz) return value.charAt(0); if (Character.TYPE == clazz) return value.charAt(0); return value; } }