「java判断是否基本类型」java 判断基本类型

博主:adminadmin 2022-11-21 17:26:08 57

本篇文章给大家谈谈java判断是否基本类型,以及java 判断基本类型对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java判断是不是基本类型,基本类型与对应的包装类

isPrimitive() 可以判断是否为基本类型

完整为:aClass.getMethod(getMethodName).getReturnType().isPrimitive()

包装类可以为:Number.class.isAssignableFrom(methodReturnType) 重点是isAssignableFrom()方法

「java判断是否基本类型」java 判断基本类型

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

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

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

2、instanceof关键字判断;

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

举例如下:

package com.cxyapi.oo;  

  

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

 * @author cxy @  

 */  

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(""));  

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

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

    }  

}  

  

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

class TypeObject{}

自定义的类型识别工具:

package com.cxyapi.oo;  

  

import java.util.HashMap;  

import java.util.Map;  

  

/** 类型识别工具 

 * @author cxy @  

 */  

public class TypeTools  

{  

    //获得类型  

    public static MapString,String getType(Object o)  

    {  

        MapString,String typeInfo=new HashMapString,String();  

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

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

        return typeInfo;  

    }  

      

    public static MapString,String getType(int i)  

    {  

        MapString,String typeInfo=new HashMapString,String();  

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

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

        return typeInfo;  

    }  

      

    public static MapString,String getType(long l)  

    {  

        MapString,String typeInfo=new HashMapString,String();  

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

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

        return typeInfo;  

    }  

      

    public static MapString,String getType(boolean b)  

    {  

        MapString,String typeInfo=new HashMapString,String();  

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

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

        return typeInfo;  

    }  

      

    public static MapString,String getType(char b)  

    {  

        MapString,String typeInfo=new HashMapString,String();  

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

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

        return typeInfo;  

    }  

      

    public static MapString,String getType(float f)  

    {  

        MapString,String typeInfo=new HashMapString,String();  

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

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

        return typeInfo;  

    }  

      

    public static MapString,String getType(double d)  

    {  

        MapString,String typeInfo=new HashMapString,String();  

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

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

        return typeInfo;  

    }  

      

    public static MapString,String getType(String s)  

    {  

        MapString,String typeInfo=new HashMapString,String();  

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

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

        return typeInfo;  

    }  

      

}

Java判断变量是否是基本类型以及是否是默认值

具体方法如下:

1、判断变量是否是基本类型:

2、判断是否是默认值:

java判断是否基本类型的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 判断基本类型、java判断是否基本类型的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-21,除非注明,否则均为首码项目网原创文章,转载请注明出处。