Java String Value Of valueOf(final Class type, final String value)

Here you can find the source of valueOf(final Class type, final String value)

Description

Convert a string value to a boxed primitive type.

License

Open Source License

Parameter

Parameter Description
type the boxed primitive type
value the string value

Return

a boxed primitive type

Declaration

public static Object valueOf(final Class<?> type, final String value) 

Method Source Code

//package com.java2s;

public class Main {
    /**/* w  w  w.  j av a2  s  .c  om*/
     * Convert a string value to a boxed primitive type.
     *
     * @param type the boxed primitive type
     * @param value the string value
     * @return a boxed primitive type
     */
    public static Object valueOf(final Class<?> type, final String value) {
        if (type == String.class) {
            return value;
        }
        if (type == boolean.class) {
            return Boolean.valueOf(value);
        }
        if (type == int.class) {
            return Integer.valueOf(value);
        }
        if (type == long.class) {
            return Long.valueOf(value);
        }
        if (type == float.class) {
            return Float.valueOf(value);
        }
        if (type == double.class) {
            return Double.valueOf(value);
        }
        throw new IllegalArgumentException("Unsupported type " + type.getName());
    }
}

Related

  1. valueOf(Class enumType, String name)
  2. valueOf(Class enumType, String name)
  3. valueOf(Class enumType, String name)
  4. valueOf(Class enumType, String name, T defaultValue)
  5. valueOf(final Class enumType, final String value)
  6. valueOf(final Class clazz, final String name)
  7. valueOf(final Class enumType, final String name)
  8. valueOf(final Object obj, final String defaultValue)
  9. valueOf(Object object, String defaultEmptyValue)