【Java】Java中Integer和int比较大小出现的错误
来源:互联网 发布:淘宝客服忙吗 编辑:程序博客网 时间:2024/06/05 18:00
Java的Integer类有一个内部类,缓存着一个常量池.
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) { try { 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); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {}}
范围在 -128 ~ 127 之间(包括),直接用==
Integer i = 100;int j = 100;return i == j;//true
这里返回的是true.
但是 >127 或者 <-128
Integer i = 500;int j = 500;return i == j; //false
所以当需要判断的Integer不在这个范围的时候,最好用parseInt()方法转换成int后再去比较大小
阅读全文
0 0
- 【Java】Java中Integer和int比较大小出现的错误
- java中int和integer比较,integer和integer比较
- Java中int和Integer关系比较
- Java中int和Integer比较问题
- Java中int和Integer关系是比较微妙的
- Integer和int的比较大小
- Integer和int大小比较
- Java中int 和 Integer 的区别
- Java中int和Integer的区别
- Java中Integer和int的区别
- Java中int和Integer的区别
- Java中int和Integer的区别
- java中 int 和 Integer 的区别
- Java中int和Integer的区别
- Java中int和Integer的区别
- Java中integer和int的区别
- Java中int和Integer的区别
- Java中int和Integer的区别
- 32位Linux系统虚拟地址映射
- 欢迎使用CSDN-markdown编辑器
- Spdylay
- webjs--获取上传图片的缓存路径展示在页面上
- sync fence 软件接口-------------sw_sync_timeline和sw_sync_pt
- 【Java】Java中Integer和int比较大小出现的错误
- 好的学习资源网站
- 反射的代码
- 二维数组操作
- 好吧,第一篇博文,盖个章,留个印
- 欢迎使用CSDN-markdown编辑器
- 剑指offer:第44题扑克牌的顺子
- HTTP与HTTPS的概念和区别
- 【深度长文】人脸识别:人脑认知与计算机算法(五部曲)