傻傻分不清?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是什么?两者有什 ...
随机推荐
- SQL 使用分区方法
- 30 分钟理解 CORB 是什么
写在前面 前些日子在调试 bug 的时候,偶然发现这么一个警告: Cross-Origin Read Blocking (CORB) blocked cross-origin response htt ...
- 小程序获取Unionid
小程序获取用户Unionid,必须授权获取密文.但授权成功后不是永久的.除非关注了公众号或者App微信绑定了, 解决办法是通过code获取openid,然后用openid去数据库查对应的Unionid ...
- Luogu P2309 loidc,卖卖萌
题目链接:Click here 题目大意:给你一个长度为n的数串,问这个数串的sum为正数的子串个数 Solution: 我们先处理以下前缀和,记为\(s_i\) 则问题可以转化为求有多少对\(i,j ...
- ArrayList遍历的三种方法
在输出很多的ArrayList的元素时,用普通的for循环太麻烦,因此本文介绍三种遍历ArrayList的方法 package test; public class Student { private ...
- Ambari显示server 返回500 error
Ambari server 搭建过程中到了revicw环境遇到点击deploy:发现页面没有响应 Console显示server 返回500 error错误,页面中没有提示更多的报错信息. 经过日志查 ...
- [LeetCode]-DataBase-Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- 关于判断StringBuffer是否为空
对于String和StringBuffer来说,都是通过创建新的char value[]数组来达到字符串改变的操作的,只不过String是通过新创建String对象来达到目的, 而StringBuff ...
- onCreateViewHolder方法加载了子项的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_w ...
- learning webrtc 使用node.js
第二章 有使用node.js创建静态服务器的步骤 不过不够详细 下面以Windows为例 1.到官方网站下载安装包 然后安装 2.用管理员权限启动命令行 3.命令行窗口执行npm config set ...