Java中怎样判断一个变量是否属于哪种类型

Python024

Java中怎样判断一个变量是否属于哪种类型,第1张

变量类型识别有3种方法:

1、通过反射拿到变量的类型;

2、instanceof关键字判断;

3、通过java的多态(方法重载)来DIY类型识别。

举例如下:

package com.cxyapi.oo  

  

/** 类型识别工具测试类 

 * @author cxy @ www.cxyapi.com 

 */  

public class TypeToolsTest  

{  

    public static void main(String[] args)  

    {  

        int i=0  

        TypeObject to=new TypeObject()  

        //1.反射  

        System.out.println("to的类型:"+to.getClass().getSimpleName())  

        System.out.println(int.class.getSimpleName())  

        System.out.println(Integer.class.getSimpleName())  

        //但是对于一个不确定类型的基本数据类型变量我们没法用反射来获取其类型。  

        System.out.println("----------------------")  

          

        //2.instanceof  

        if(to instanceof TypeObject){ System.out.println("to是TypeObject类型的")}  

        //但是这种办法貌似也没法确定基本数据类型  

        System.out.println("----------------------")  

          

        //以上两种方式对于对象,引用类型的都很好用,但是对基本数据类型就不那么好用了。  

        //3.通过多态(方法的重载)  

        System.out.println("i是:"+TypeTools.getType(i))  

        System.out.println("to是:"+TypeTools.getType(to))  

        System.out.println("\"cxyapi\"是:"+TypeTools.getType("www.cxyapi.com"))  

        //可以看出来 最后一种方式使用多态的方式达到了检测类型(基本类型和引用类型)的目的  

        //除了弥补其他两种方式不能检测基本数据类型的不足在外,还能自己DIY类型信息  

    }  

}  

  

//定义一个类,为了演示引用类型的类型检测  

class TypeObject{}

自定义的类型识别工具:

package com.cxyapi.oo  

  

import java.util.HashMap  

import java.util.Map  

  

/** 类型识别工具 

 * @author cxy @ www.cxyapi.com 

 */  

public class TypeTools  

{  

    //获得类型  

    public static Map<String,String> getType(Object o)  

    {  

        Map<String,String> typeInfo=new HashMap<String,String>()  

        typeInfo.put("类型", o.getClass().getSimpleName())  

        typeInfo.put("描述", "引用类型")  

        return typeInfo  

    }  

      

    public static Map<String,String> getType(int i)  

    {  

        Map<String,String> typeInfo=new HashMap<String,String>()  

        typeInfo.put("类型", "int")  

        typeInfo.put("描述", "整形")  

        return typeInfo  

    }  

      

    public static Map<String,String> getType(long l)  

    {  

        Map<String,String> typeInfo=new HashMap<String,String>()  

        typeInfo.put("类型", "long")  

        typeInfo.put("描述", "长整型")  

        return typeInfo  

    }  

      

    public static Map<String,String> getType(boolean b)  

    {  

        Map<String,String> typeInfo=new HashMap<String,String>()  

        typeInfo.put("类型", "boolean")  

        typeInfo.put("描述", "布尔类型")  

        return typeInfo  

    }  

      

    public static Map<String,String> getType(char b)  

    {  

        Map<String,String> typeInfo=new HashMap<String,String>()  

        typeInfo.put("类型", "char")  

        typeInfo.put("描述", "字符")  

        return typeInfo  

    }  

      

    public static Map<String,String> getType(float f)  

    {  

        Map<String,String> typeInfo=new HashMap<String,String>()  

        typeInfo.put("类型", "float")  

        typeInfo.put("描述", "单精度浮点型")  

        return typeInfo  

    }  

      

    public static Map<String,String> getType(double d)  

    {  

        Map<String,String> typeInfo=new HashMap<String,String>()  

        typeInfo.put("类型", "double")  

        typeInfo.put("描述", "双精度浮点型")  

        return typeInfo  

    }  

      

    public static Map<String,String> getType(String s)  

    {  

        Map<String,String> typeInfo=new HashMap<String,String>()  

        typeInfo.put("类型", "String")  

        typeInfo.put("描述", "字符串类型")  

        return typeInfo  

    }  

      

}

先得到对象的类字节码Class对象,通过Class对象再得到类型,而后进行判断是不原始类型方法可能是isPrimitive或数组isArray()。

爪哇是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。

Java自面世后就非常流行,发展迅速,对C++语言形成了有力冲击。Java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。