您当前的位置: 首页 >  Java

蓝不蓝编程

暂无认证

  • 4浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

一些不常见的java特性

蓝不蓝编程 发布时间:2014-05-09 22:54:54 ,浏览量:4

1.java基础类型对应的包装类型是带有缓存功能的

   1)Integer/Short/Long 会对-128到127的值进行缓存;

   2)Character 会对0到127的值进行缓存

   Float、Double没有此功能

  为什么说Integer类带有缓存功能,见java源代码Integer.java:

  其内部包含一个静态类IntegerCache,会实例化一个-128到127的Integer数组;

    private static class IntegerCache {
	private IntegerCache(){}

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

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

在调用Integer.valueOf函数时就会使用到这个缓存功能,如果值在-128到127,则直接从缓存的Integer数组中取值

public static Integer valueOf(int i) {
	final int offset = 128;
	if (i >= -128 && i             
关注
打赏
1639405877
查看更多评论
0.0372s