java获取对象类型

public static  void main(String args[]) {
        //String var = "123";
        int var = -120;
        //TODO:判断数据类型
        Object param = var;


        if (param instanceof Integer) {
            int value = ((Integer) param).intValue();
            System.out.println("var ls Integer!");
            System.out.println("var ls value ==" + value);
        } else if (param instanceof String) {
            String s = (String) param;
            System.out.println("var ls String!");
            System.out.println("var ls value ==" + s);
        } else if (param instanceof Double) {
            double d = ((Double) param).doubleValue();
            System.out.println("var ls double!");
            System.out.println("var ls value ==" + d);
        } else if (param instanceof Float) {
            float f = ((Float) param).floatValue();
            System.out.println("var ls float!");
            System.out.println("var ls value ==" + f);
        } else if (param instanceof Long) {
            long l = ((Long) param).longValue();
            System.out.println("var ls long!");
            System.out.println("var ls value ==" + l);
       
        } else if (param instanceof Boolean) {
            boolean b = ((Boolean) param).booleanValue();
            System.out.println("var ls boolean!");
            System.out.println("var ls value ==" + b);
        } else if (param instanceof Date) {
            Date d = (Date) param;
            System.out.println("var ls Date!");
            System.out.println("var ls value ==" + d);
        }

        //以上代码为获得Object的类型


    }

你可能感兴趣的:(java获取对象类型)