Interger声明的在-128 ~ 127之间值比较为true
2016-03-25 11:02
295 查看
Integer f1=100,f2=100,f3=189,f4=189;
System.out.println(f1==f2);
System.out.println(f3==f4);
输出 true false
源码:
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
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() {}
}
Interger 实现的时候 对于-128 到127之间的值做了缓存处理 (IntegerCache ),源码见上。
System.out.println(f1==f2);
System.out.println(f3==f4);
输出 true false
源码:
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
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() {}
}
Interger 实现的时候 对于-128 到127之间的值做了缓存处理 (IntegerCache ),源码见上。
相关文章推荐
- 关于button — 点击变换背景图片
- ${pageContext.request.contextPath}的作用
- X5的UI部分和传统Web页面开发的差异
- IOS开发UI篇--使用CAShapeLayer实现复杂的View的遮罩效果
- unigui中TUniDBEdit的OnEndDrag问题
- iOS UILabel设置行间距和字间距并计算高度
- iOS开发基础 - UINavigationcontroller
- iOS开发基础 - UISearchController
- iOS开发基础 - UITableView
- iOS开发基础 - UICollectionView
- iOS开发基础 - UITouch的使用
- iOS-为UITableViewCell添加详情指示图,和说明文字
- iOS开发基础 - UITabBarController
- iOS开发基础-UIPageControl
- iOS开发基础- UIScrollView
- iOS开发基础-UIView的使用
- UIMenuController 实现长按显示自定义菜单功能
- iOS开发基础-UITextView&UITextField
- iOS开发基础-UIImageView&&UIImage的使用
- iOS开发基础-UIButton的使用