Java Object Create toObject(Class clazz, String value)

Here you can find the source of toObject(Class clazz, String value)

Description

This method parses the given value into the specified primitive or wrapper class.

License

Open Source License

Parameter

Parameter Description
clazz - primitive or wrapper class used to parse
value - string to be parsed

Return

object of type clazz parsed from the string

Declaration

public static Object toObject(Class<?> clazz, String value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from  w  w w . jav  a2  s . co  m*/
     * 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;
    }
}

Related

  1. as(Object value, Class type)
  2. toObject(boolean[] array)
  3. toObject(byte[] array)
  4. toObject(byte[] byteArray)
  5. toObject(byte[] primitiveArray)
  6. toObject(double[] a)
  7. toObject(final int i)
  8. toObject(int[] array)
  9. toObject(int[] values)