傻傻分不清?Integer、new Integer() 和 int 的面试题
1、Integer 是 int 的包装类,int 则是 java 的一种基本数据类型
2、Integer 变量必须实例化后才能使用,而int变量不需要
3、Integer 实际是对象的引用,当new一个 Integer时,实际上是生成一个指针指向此对象;而 int 则是直接存储数据值
4、Integer的默认值是null,int的默认值是0
1、两个 new Integer() 变量比较 ,永远是 false
因为new生成的是两个对象,其内存地址不同
Integer i = newInteger(100);
Integer j = newInteger(100);
System.out.print(i == j); //false
2、Integer变量 和 new Integer() 变量比较 ,永远为 false。因为 Integer变量 指向的是 java 常量池 中的对象,
而 new Integer() 的变量指向 堆中 新建的对象,两者在内存中的地址不同。
Integer i = newInteger(100);
Integer j = 100;
System.out.print(i == j); //false
3、两个Integer 变量比较,如果两个变量的值在区间-128到127 之间,则比较结果为true,如果两个变量的值不在此区间,则比较结果为 false 。
Integer i = 100;
Integer j = 100;
System.out.print(i == j); //true
Integer i = 128;
Integer j = 128;
System.out.print(i == j); //false
分析:Integer i = 100 在编译时,会翻译成为 Integer i = Integer.valueOf(100),而 java 对 Integer类型的 valueOf 的定义如下:
publicstaticInteger valueOf(int i){
assertIntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high){
returnIntegerCache.cache[i + (-IntegerCache.low)];
}
returnnewInteger(i);
}
java对于-128到127之间的数,会进行缓存。所以 Integer i = 127 时,会将127进行缓存,下次再写Integer j = 127时,就会直接从缓存中取,就不会new了。
4、 int 变量 与 Integer、 new Integer() 比较时,只要两个的值是相等,则为true
因为包装类Integer 和 基本数据类型int 比较时,java会自动拆包装为int ,然后进行比较,实际上就变为两个int变量的比较。
Integer i = newInteger(100); //自动拆箱为 int i=100; 此时,相当于两个int的比较
int j = 100;
System.out.print(i == j); //true
示例1:
publicclassIntegerDemo {
publicstaticvoid main(String[] args) {
int i = 128;
Integer i2 = 128;
Integer i3 = newInteger(128);
System.out.println("i == i2 = " + (i == i2)); // Integer会自动拆箱为int,所以为true
System.out.println("i == i3 = " + (i == i3)); // true,理由同上
Integer i4 = 127;// 编译时被翻译成:Integer i4 = Integer.valueOf(127);
Integer i5 = 127;
System.out.println("i4 == i5 = " + (i4 == i5));// true
Integer i6 = 128;
Integer i7 = 128;
System.out.println("i6 == i7 = " + (i6 == i7));// false
Integer i8 = newInteger(127);
System.out.println("i5 == i8 = " + (i5 == i8)); // false
Integer i9 = newInteger(128);
Integer i10 = newInteger(128);
System.out.println("i9 == i10 = " + (i9 == i10)); // false
}
}
答案是
i == i2 = true
i == i3 = true
i4 == i5 = true
i6 == i7 = false
i5 == i8 = false
i9 == i10 = false
示例2:
package demo1.demo1;
publicclassCampare {
publicstaticvoid main(String[] args) {
Integer a = newInteger(127), b = newInteger(128);
int c = 127, d = 128, dd = 128;
Integer e = 127, ee = 127, f = 128, ff = 128;
System.out.println(a == b); // false 因为a,b都是new出来的对象,地址不同所以为false
System.out.println(a == c); // true a会自动拆箱为int类型
System.out.println(a == e); // false 指向的地址不同a是new出来的
System.out.println(e == c); // true e会自动拆箱为int类型
System.out.println(e == ee);// true Integer对 处于-128到127范围之间,指向了同一片地址区域
System.out.println(b == f); // false 指向的地址不同b是new出来的
System.out.println(f == d); // true f自动拆箱为int类型
System.out.println(f == ff); /*
* false 指向的不是同一片地址区域。
* 在Integer类型中,-128到127存放的是同一片区域地址,
* 之外的数是另外开辟空间来进行 存储的
*/
System.out.println(d == dd); // true 不解释
}
}
示例3:
Integer i01 = 59;
int i02 = 59;
Integer i03 =Integer.valueOf(59);
Integer i04 = newInteger(59);
以下输出结果为false的是:
System.out.println(i01== i02);
System.out.println(i01== i03);
System.out.println(i03== i04);
System.out.println(i02== i04);
解析:
i01 == i02 。i01.intValue()i02 两个值的比较5959 -->true;
i01 == i03 。由于 59在-128到127之间,所以,i01和i03的赋值操作返回的是同一个对象。都是从chche中返回的同一个对象,对象地址相同 true;
i03 == i04。i03是来自缓存值,i04是新new的对象 ,二者不是同一个对象,所以false。
i02 == i04。和第一个类似,true。
答案是 C 。
示例4:与示例3的唯一不同,就是将值全部改成128。
Integer i01 = 128;
int i02 = 128;
Integer i03 = Integer.valueOf(128);
Integer i04 = newInteger(128);
以下输出结果为false的是:
System.out.println(i01 == i02);
System.out.println(i01 == i03);
System.out.println(i03 == i04);
System.out.println(i02 == i04);
答案:
true
false
false
true
作者:chenxiangxiang
来源:cnblogs.com/cxxjohnson/p/10504840.html
2、
3、
4、
5、
点击「阅读原文」和栈长学更多~
傻傻分不清?Integer、new Integer() 和 int 的面试题的更多相关文章
- Java中Integer.parseInt和Integer.valueOf,你还傻傻分不清吗?
在Java的Integer类中,有Integer.valueOf(String s)和Integer.parseInt(String s)两个静态方法,他们都能够将字符串转换为整型,他们到底有什么区别 ...
- [转帖]十分钟快速理解DPI和PPI,不再傻傻分不清!
十分钟快速理解DPI和PPI,不再傻傻分不清! https://baijiahao.baidu.com/s?id=1605834796518990333&wfr=spider&for= ...
- 学点经济学:M0、M1、M2、M3,傻傻分不清?(转载)
来源:http://t.10jqka.com.cn/pid_97006727.shtml 学点经济学:M0.M1.M2.M3,傻傻分不清? 25,508人浏览 2018-08-03 11:06 常听人 ...
- ASCII、Unicode、UTF-8、UTF-8(without BOM)、UTF-16、UTF-32傻傻分不清
ASCII.Unicode.UTF-8.UTF-8(without BOM).UTF-16.UTF-32傻傻分不清 目录 ASCII.Unicode.UTF-8.UTF-8(without BOM). ...
- 【jvm】08-垃圾回收器那么多傻傻分不清?
[jvm]08-垃圾回收器那么多傻傻分不清? 欢迎关注b站账号/公众号[六边形战士夏宁],一个要把各项指标拉满的男人.该文章已在github目录收录. 屏幕前的大帅比和大漂亮如果有帮助到你的话请顺手点 ...
- MVP MVC MVVM 傻傻分不清
最近MVC (Model-View-Controller) 和MVVM (Model-View-ViewModel) 在微软圈成为显学,ASP.NET MVC 和WPF 的Prism (MVVM Fr ...
- OCA,OCP,OCM傻傻分不清?
可能大家知道OCA.OCP.OCM的关系是一个比一个难考,一个比一个含金量高,但是你知道具体的考试科目.考试方式.就业形势区别吗?不知道的话这篇通俗易懂的文章会让你一目了然. 区别一:含金量 ■OCA ...
- session cookie傻傻分不清
做了这么多年测试,还是分不清什么是cookie,什么是session?很正常,很多初级开发工程师可能到现在都搞不清什么是session,cookie相对来说会简单很多. 下面这篇文章希望能够帮助大家分 ...
- 【华为敏捷/DevOps实践】7. 敏捷,DevOps,傻傻不分清楚【华为云技术分享】
文:姚冬(华为云DevCloud首席技术布道师,资深DevOps与精益/敏捷专家,金融解决方案技术Leader,中国DevOpsDays社区核心组织者) 前言 敏捷是什么?DevOps是什么?两者有什 ...
随机推荐
- linux下yum安装python3
linux下yum安装python3 linux下yum安装python3yum install python34 -ypython3 --version wget --no-check-certif ...
- TeamViewer的替代品:realVNC
TeamViewer的替代品:realVNC official web: realvnc: https://www.realvnc.com/ steps: 在需要被控制的PC上装上realVNC的服务 ...
- linux-shell脚本基础-2
1,用户组 添加用户 useradd -u UID -o -g 指定 GID或组名 -c 注释信息 -d 家目录 -s shell -G 附加组 -r 系统用户 -m 家目录,系统用户 -M 不创建家 ...
- Mac 安装 MongoDB 数据库
1. 使用 brew install mongodb 安装 (参见下图) 2. 安装成功如下图 (成功与否可参考 方框内字符) 3. 启动 MongoDB 数据库 3.1 先创建数据库存储目录 /da ...
- Mysql 获取成绩排序后的名次
其实就是输出mysql的排序后的行号 RT:获取单个用户的成绩在所有用户成绩中的排名 可以分两步: 1.查出所有用户和他们的成绩排名 ) as rowNo from t_user, () ) ...
- Spring Boot教程(二十一)开发Web应用(2)
在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过Thymeleaf渲染一个页面. @Controller public class HelloController { ...
- sqli-labs(36)
0X01发现又是‘’被过滤了 ?id=%df%%20and%=%23 错误 ?id=1%df%27%20and%201=1%23 正确 存在注入 0X01爆数据库 ?id=-%df%%20unio ...
- 同一个tomcat部署多个项目
在开发项目中,有时候我们需要在同一个tomcat中部署多个项目,小编之前也是遇到了这样的情况,碰过不少的壁,故整理总结如下,以供大家参考.(以Linux为例,其他系统同样适用) 一.首先将需要部署的项 ...
- 终于, Delphi XE2 携带 GDI+ 库了
终于, Delphi XE2 携带 GDI+ 库了 使用了较早的 http://www.progdigy.com uses Winapi.GDIPAPI, Winapi.GDIPOBJ{, Winap ...
- Dataframe的索引问题
1 两个Dataframe相加时,一定要注意索引是否对应再相加,利用这个特点有时可以先用set_index()将某些列置为索引列,再进行相加. import pandas as pd df1 = pd ...