
Integer比较值面试题总结
发布日期:2021-05-07 11:59:36
浏览次数:26
分类:技术文章
本文共 2959 字,大约阅读时间需要 9 分钟。
Integer比较值面试题总结
代码:
public static void main(String[] args) { int a = 127; Integer a1 = 127; Integer a2 = 127; System.out.println(a == a1);//true System.out.println(a == a2);//true System.out.println(a1 == a2);//true Integer a3 = 128; Integer a4 = 128; Integer a5 = new Integer(128); System.out.println(a3 == a4);//false System.out.println(a3 == a5);//false System.out.println(a1.equals(a));//true System.out.println(a1.equals(a2));//true System.out.println(a3.equals(a4));//true }
解析
上面代码包含的知识点有:
- ==与equals的区别;
- Integer.valueOf()源码;
- 基本数据类型的拆箱和装箱;
首先,==比较的是两个对象的地址值(基本数据类型比较的是值,对象比较的是地址值);在没有重写equals方法的情况下,equals方法和 ==方法作用一致,如果重写了equals方法,那么根据重写的代码会实现不同的功能,例如String类就重写了equals方法(比较的是两个对象的值),还有包装类等等:
String s1 = new String("ab");String s2 = new String("ab");System.out.println(s1.equals(s2));//true
其次,关于Integer.valueOf()的源码分析:
valueOf()源码:
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i);}
IntegerCache源码:
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() { } }
如下代码:
Integer a1 = 127; Integer a2 = 127; System.out.println(a == a1);//true System.out.println(a == a2);//true System.out.println(a1 == a2);//false
127是基本数据类型,Integer a1 = 127;等同于Integer a1 = Integer.valueOf(127);通过上面的源码得知,如果Integer.valueOf(int number) 该number的范围在[-128,127]之间,那么该值会存储在IntegerCache.cache[]数组中,而不重新进行分配空间;所以a1 == a2;
如果该值超过该范围,例如:Integer s3 = 128; Integer s4 = 128; System.out.println(s3 == s4);//false
运行结果为false,堆会为其重新分配内存空间。**注明:**对于Integer数据类型,[-128,127]数据存储在方法区,其他的数据存储在堆中。
自动拆箱装箱,也就是说当基本数据类型和包装类型进行比较的时候,包装数据类型会进行自动拆箱;所以相当于基本数据类型比较值是否相等。
凡是两个经过new的对象进行比较,都是false;例如:Integer a1 = new Integer(12);Integer a2 = new Integer(12);System.out.println(a1 == a2);//false
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年03月26日 20时21分59秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
vue源码分析(MVVM篇)
2019-03-04
React(八)- ReactUI组件库及Redux的使用
2019-03-04
TypeScript系列文章导航
2019-03-04
base64编码字符串和图片的互转
2019-03-04
linux 下安装kolla报错 提示Cannot uninstall requests
2019-03-04
设计模式之组合模式
2019-03-04
(Python学习笔记):字典
2019-03-04
(C++11/14/17学习笔记):线程启动、结束,创建线程多法、join,detach
2019-03-04
leetcode 14 最长公共前缀
2019-03-04
做做Java
2019-03-04
map的find函数和count函数
2019-03-04
C++并发与多线程(一)
2019-03-04
7628 EDCCA认证寄存器修改(认证自适应)
2019-03-04
计算机网络子网划分错题集
2019-03-04
java一些基本程序
2019-03-04
神经元与神经网络一之概述
2019-03-04
FANUC机器人R-30iB_R-30iB PLUS备件规格型号统计整理
2019-03-04
FANUC机器人的镜像备份操作及U盘格式化具体步骤
2019-03-04
vue-依赖-点击复制
2019-03-04