傻傻分不清?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是什么?两者有什 ...
随机推荐
- Vue结合webpack实现路由懒加载和分类打包
https://blog.csdn.net/weixin_39205240/article/details/80742723
- python中json对象转换出错解决方法
今天在使用python中的json转换碰到一个问题: 接收一个post的json字符串: s={"username":"admin","passwor ...
- void 运算符和 逗号运算符
一.void 运算符 void 运算符的作用目的是 执行一个表达式,但是不用返回任何值,或者是返回undefined void 本身就有 无效.空的 的意思. void运算符的用法: 1.不加括号的写 ...
- CentOS7安装codeblocks
1.yum -y install epel-release 2.yum clean all && yum makecache 3.yum -y install gtk2-devel c ...
- 浅谈BSGS(大步小步)及其扩展
用途: 一般用来求\(a^x\equiv b\,\,(mod\,p)\)的最小正整数解,其中gcd(a,p)=1 设\(u=\lceil sqrt(p)\rceil\),则式子可以转化为\(a^{iu ...
- 【POJ2992】Divisors
[题目概括] 计算\(C_n^k\)的因子个数. [思路要点] 首先考虑将组合数展开,展开后就是\(\frac {n!}{k!\times (n-k)!}\). 这样就是计算出这些质因子的个数,然后将 ...
- cpp 面向对象初步探索
需求 尝试定义一个complex(复数类) 简略实现 headers/complex.h #ifndef __COMPLEX__ #define __COMPLEX__ class complex { ...
- JAVA语言动手动脑问题
1. 早期经常这样定义变量 int value=100;前面的示例中这样定义变量 MyClass obj = new MyClass(); 这两种方式定义的变量是一样的吗? 答:不一样,后者开 ...
- Shell 变量/echo命令
Shell 教程 Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面,用户 ...
- Spring Boot教程(九)异步方法
创建工程 在pom文件引入相关依赖: <dependency> <groupId>org.springframework.boot</groupId> <ar ...