return the init value for given type. - Android java.lang.reflect

Android examples for java.lang.reflect:Class

Description

return the init value for given type.

Demo Code


//package com.java2s;

public class Main {
    /**/*  w  w w  . ja  v  a  2s .  c  o  m*/
     * return the init value for given type.
     * 0 for byte, short, int, long, float and double.
     * '\0' for char,
     * false for boolean,
     * null for others.
     */
    public static String getInitValue(Class<?> type) {
        if (byte.class.equals(type) || short.class.equals(type)
                || int.class.equals(type) || long.class.equals(type)
                || float.class.equals(type) || double.class.equals(type)) {
            return "0";
        } else if (char.class.equals(type)) {
            return "'\\0'";
        } else if (boolean.class.equals(type)) {
            return "false";
        } else {
            return "null";
        }
    }
}

Related Tutorials