`
helloqyq
  • 浏览: 2095 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

针对java中Integer的一些缓存

 
阅读更多
public class JavaTest {
public static void main(String[] args){
Integer a=50;
Integer b=50;
System.out.println("1::"+(a==b));
System.out.println("2::"+(a>=b));
System.out.println("3::"+(a<=b));

Integer c=500;
Integer d=500;
System.out.println("11::"+(c==d));
System.out.println("22::"+(c>=d));
System.out.println("33::"+(c<=d));
}
}


打印结果::

1::true
2::true
3::true
11::false
22::true
33::true


原因:

/**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param  i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since  1.5
*/


public static Integer valueOf(int i) {
    if (i >= -128 && i <= IntegerCache.high)
        return IntegerCache.cache[i + 128];
    else
        return new Integer(i);
}

你看懂了吗?
分享到:
评论
2 楼 helloqyq 2011-12-31  
linuxzhang 写道
你写的代码不报错的?

没有啊!怎么错啦!
1 楼 linuxzhang 2011-12-31  
你写的代码不报错的?

相关推荐

Global site tag (gtag.js) - Google Analytics