JDK - 8种基本类型,和包装类

8种基本数据类型

   long double int float byte char short boolean(栈)    

字符 char

数值  整形: long int byte short 浮点型:double float 

布尔 boolean


 

包装类在jdk中处理方式

 Long Double Integer Float ByteCharacter Short Boolean(堆)

Integer  32 -2147483648 ~ 2147483647

JDK源码

/*
  * IntegerCache内部类
  * 其中cache[]数组用于存放从-128到127一共256个整数
  */  
 private static class IntegerCache {
        static final int high;
        static final Integer cache[];

        static {
            final int low = -128;
            // high value may be configured by property
            int h = 127;
            if (integerCacheHighPropValue != null) {
                // Use Long.decode here to avoid invoking methods that
                // require Integer's autoboxing cache to be initialized
                int i = Long.decode(integerCacheHighPropValue).intValue();
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - -low);
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }




public static Integer valueOf(int i) { 

        if(i >= -128 && i <= IntegerCache.high)

            //进行缓存

            return IntegerCache.cache[i + 128];

       else

            //新的对象

            return new Integer(i);

}

   当if为true时会获取缓存的值else实例化一个新的Integer对象 

   

       Integer i1 = 127;

   Integer i2 = 127;

   System.out.println(i1 == i2);

 处理过程

     1>  Integer.valueOf(127)

     2>  当值大于等于 -128 <= 127 时会调用IntegerCache.cache[i + 128]

   

       Integer i1 = 128;

   Integer i2 = 128;

   System.out.println(i1 == i2);

 处理过程

     1>  Integer.valueOf(128)

     2>  new Integer(128)

结果 :1true

       因为当值在-128 127之间时那么Integer.valueOf

       会从IntegerCache.cache缓存中获取,那么获得的就是该数组下

       同一个引用对象,判断堆中的地址是否一样

       2 false

       因为不在-128 127之间,虚拟机会实例化两个不同Integer对象

       如果想比较堆中i1,i2对象的内容是否一致时,使用equals来进行判断

 总结Integer取值范围在-128 ~ 127 之间时那么每次都会从IntegerCache.cache数组缓存中获得,不会每次都去实例化一个Integer对象,也就是说不会开辟一个新的内存空间,这样在性能方面也是一个很大的优化.

3

Integer i1 = 128;

System.out.println(i1 == 128);

处理过程

     1>  Integer.valueOf(128) (i1) 

     2>  new Integer(128)

     3>  i1.intValue()

结果: true

      (i1 == 128) 因为128int类型 这个时候Integer对象会进行拆箱操作,然后在做比较,那么它们比较的,就不是堆中的地址,而是判断数值是否是一样。

Long和Integer处理类似 64 -9223372036854775808 ~ 9223372036854775807

JDK源码

public static Long valueOf(long l) {

final int offset = 128;

if (l >= -128 && l <= 127) { // will cache

    return LongCache.cache[(int)l + offset];

}

        return new Long(l);

    }

 /*
  * LongCache内部类
  * 其中cache[]数组用于存放从-128到127一共256个整数
  */  
  private static class LongCache {
    private LongCache(){}

    static final Long cache[] = new Long[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
        cache[i] = new Long(i - 128);
    }
    }

 longValue() 拆箱

Float    32位 -3.40292347E+38 ~ 3.40292347E+38 E表示10N38)次方)

  例1

     Float f1 = 12.5F;

  Float f2 = 12.5F;

  System.out.println(f1 == f2);

处理过程

      1>  Float.valueOf(12.5)      

      2>  new Float(12.5)

  例2

     Float f1 = 12.5F;

    float f2= 12.5F;

  System.out.println(f1 == f2);

处理过程 

      1>  Float.valueOf(12.5)      

      2>  new Float(12.5)

      3>  i1.intValue() 

 例3

     Float f1 = 12.5F; 

     Float f2 = 12.5F;

  System.out.println(f1.equals(f2));

处理过程   

      1>  Float.valueOf(12.5)   

      2>  new Float(12.5

结果

   1 false  

   因为比较是堆中地址,Float 类型每次都会进行实例化,所以两个在堆中地址是不一样的

   例2 true 

   因为 f2 是基本数据类型,所以f1首先会进行拆箱操作然后在进行比较,比较的是数值

   例3 true 

   因为equals比较时f1,f2堆中内容,是否一样

Double  64位 -1.79769313486231570E+38 ~ 1.79769313486231570E+38 E表示10N38)次方)    

    处理逻辑同Float类似

Short   16 -32768 ~ 32767

   处理逻辑同Long类似

Byte 8位  -128  ~  127

   处理逻辑同Long类似

Character  16 /u0000 ~ /uffff

 整型和char之间可以互相转换

Boolean  true / false

public static Boolean valueOf(boolean b) {

     return (b ? TRUE : FALSE);

}


基本数据类型:存放在栈中

对象: 引用存放在栈,实际对象时存放在堆中

以上如有不对的地方请留言,让我们一起共同学习!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值