Java基本类型判断

java判断是否基本类型,基本类型与对应的包装类
2011-03-02 12:09

public class Test{
public static void main(String[] args) throws Exception {
  System.out.println((char)65);
        System.out.println(isWrapClass(Long.class));
        System.out.println(isWrapClass(Integer.class));
        System.out.println(isWrapClass(String.class));
        System.out.println(isWrapClass(Test.class));
    }

    public static boolean isWrapClass(Class clz) {
        try {
           return ((Class) clz.getField("TYPE").get(null)).isPrimitive();
        } catch (Exception e) {
            return false;
        }
    }
}


Java的所有类总称Class,大写C.



基本类型:

Java的基本类型有八种:int, double, float, long, short, boolean, byte, char, void.

基本类型属于Class的一个子集。

因此:

.isPrimitive()是用来判断是否是基本类型的:void.isPrimitive() //true;

.class用来返回该类的Class对象的实例:int.class == intclass //基本类型

                                     int.class.isPrimitive //true

                                     Integer.class == Integer //包装类

                                     Integer.class.isPrimitive //false

.TYPE是返回基本类型的Class实例,所以:int.class == Integer.TYPE //true;

除了int Integer,类似的有 float Float; double Double; string String等。

基本类存储在栈中,包装类村住在堆中,栈是LIFO(后进先出),基本类的存取速度更快。

JAVA 虚机自动完成基本类与包装类的转换

基本类型的包装类都采用final修饰方法,因此无法继承它们扩展的新类,也无法重写它们的方法。



包装类:

包装类是基本类的扩展

int i=2, Integer i = new Integer(2); 其实这个过程是由自动转换的,定义了int i=2就不用关心将其转换为包装类的过程。这就是为什么基本类型不需要new,例如 int i = new int() i =2;是多余的。

此外,HashMap ArrayList等不能将基本类加入其中,而只能加入包装类。一个典型的实例:

Map<int, Object> testMap = new HashMap<int, Object>();就是错误的

而应该把int换成Integer。



最后,如何判断一个对象所属的类型:

常见的一个典型错误是 object.getClass.isPrimitive()
许多人指望靠此判断object是不是一个数字或者布尔。其实这样返回的是该对象的类型,如java.lang.double,而非类型的基本类型,因此是错误的。

正确的步骤是:



object instanceof String

                  Number

                  Boolean

                  Character

                  null

搞懂java的基本类型就不在会为判断对象而发愁。

 

 

 

package isAssignableForm;

public class start {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Integer a = 2;
        System.out.println(a instanceof Number);                         //t
        System.out.println(Object.class.isAssignableFrom(Number.class)); //t
        System.out.println(Integer.TYPE.isPrimitive());                  //t
        System.out.println(Integer.class.isPrimitive());                 //f
        System.out.println(int.class);                                   //int
        System.out.println(char.class);                                  //char
        System.out.println(int[].class);                                 //class [I
        System.out.println(char[].class);                                //class [C
        System.out.println(new int[0].getClass());                       //class [I
        System.out.println(new int[0].getClass().getComponentType());    //int
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值