Long类源码浅析

1、Long类和Integer相类似,都是基本类型的包装类,类中的方法大部分都是类似的;

关于Integer类的浅析可以参看:Integer类源码浅析

2、这里主要介绍一下LongCache类,该缓存类比Integer的缓存类,实现更简单

     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);
}
}

LongCache类缓存的大小不能更改了,这个大小已经固定了256;

         @Test
public void coreLong(){
//定义方法内部类
class LongCacheTest {
Long cache[] = new Long[-(-128) + 127 + 1]; public LongCacheTest(){
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
} System.out.println(Arrays.toString(new LongCacheTest().cache));
}

输出结果:[-128 ~ 127]

上一篇:Problem creating zip: Execution exce ption (and the archive is probably corrupt but I could not delete it): Java heap space -> [Help 1]


下一篇:Swift编程语言的相关资料