基本概念的区分:

1、Integer 是 int 的包装类,int 则是 java 的一种基本数据类型
2、Integer 变量必须实例化后才能使用,而int变量不需要
3、Integer 实际是对象的引用,当new一个 Integer时,实际上是生成一个指针指向此对象;而 int 则是直接存储数据值
4、Integer的默认值是null,int的默认值是0

Integernew Integer() 和 int 的比较

  • 1、两个 new Integer() 变量比较 ,永远是 false

因为new生成的是两个对象,其内存地址不同

Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j); //false
  • 2、Integer变量 和 new Integer() 变量比较 ,永远为 false。

因为 Integer变量 指向的是 java 常量池 中的对象,
而 new Integer() 的变量指向 堆中 新建的对象,两者在内存中的地址不同。

Integer i = new Integer(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 的定义如下:

public static Integer valueOf(int i){
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high){
return IntegerCache.cache[i + (-IntegerCache.low)];
}
return new Integer(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 = new Integer(100); //自动拆箱为 int i=100; 此时,相当于两个int的比较
int j = 100;
System.out.print(i == j); //true

示例1:

public class IntegerDemo {
public static void main(String[] args) {
int i = 128;
Integer i2 = 128;
Integer i3 = new Integer(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 = new Integer(127);
System.out.println("i5 == i8 = " + (i5 == i8)); // false Integer i9 = new Integer(128);
Integer i10 = new Integer(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;

public class Campare {

	public static void main(String[] args) {

		Integer a = new Integer(127), b = new Integer(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 = new Integer(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 = new Integer(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

Integer、new Integer() 和 int 比较的面试题的更多相关文章

  1. java 13-4 Integer和String、int之间的转换,进制转换

    1.int类型和String类型的相互转换 A.int -- String 推荐用: public static String valueOf(int i) 返回 int 参数的字符串表示形式. B. ...

  2. Integer.parseInt(String s, int radix)方法介绍(修正版)

    先来说明一下Integer.parseInt(String s, int radix)的功能. Integer.parseInt(String s, int radix)就是将整数字符串s(radix ...

  3. Java基础之数据比较Integer、Short、int、short

    基础很重要,基础很重要,基础很重要.重要的事情说三遍,. 今天聊一聊Java的数据比较,这个范围比较大,基础类型的比较.引用类型的比较. 前提: 1.Java和c#都提供自动装箱和自动拆箱操作,何为自 ...

  4. The expression of type Integer is unboxed into int

    问题:The expression of type Integer is unboxed into int 原因:java的包装类,方法里面要的是Integer,传入的参数确实int类型 解决方案: ...

  5. int与Integer区别+Integer类详解

    //Integer范围-128~127 //Integer与Integer比较 Integer a_127 = 127; Integer b_127 = 127; Integer c_new_127 ...

  6. Integer和Integer常量池

    Integer中有个静态内部类 IntegerCache ,里面有个cache[],也就是Integer常量池  大小为一个字节(-128-127). (jdk1.8.0_101)源码 private ...

  7. Integer两种转int方法比较

    方法一: Integer.parseInt(); 返回的是一个 int 的值. 方法二: new Integer.valueof(); 返回的是 Integer 的对象. new Integer.va ...

  8. Why Python's Integer Division Floors ---- python int(6/-132)时答案不一致,向下取整

    leetcode150题中有一个步骤: int(6/-132) == 0 or ==-1? 在自己本地python3环境跑是int(6/-132) =0,但是提交的时候确实-1. 查找相关资料解惑: ...

  9. Java.lang.Integer类中toString(int i, int radix)的具体实现

    Java.lang.Integer.toString(int i,int radix)方法可以实现将一个int类型的10进制的数据转换为指定进制的数据. api文档中介绍: 返回第二个参数指定的基数中 ...

随机推荐

  1. 不用写代码的框架 - RobotFramework+Eclispe环境安装篇

    环境安装是学习任何一个新东西的第一步,这一步没走舒坦,那后面就没有心情走下去了. 引用名句:工欲善其事必先利其器!! Robotframework:一款 自动化测试框架. Eclipse:一款编辑工具 ...

  2. 20165230 Exp3 免杀原理与实践

    目录 1.实验内容 2.基础问题回答 3.实验内容 任务一:正确使用免杀工具或技巧 使用msf编码器,msfvenom生成如jar之类的其他文件 使用veil-evasion 自己利用shellcod ...

  3. 【原创】大数据基础之Alluxio(1)简介、安装、使用

    Alluxio 1.8.1 官方:http://www.alluxio.org/ 一 简介 Open Source Memory Speed Virtual Distributed StorageAl ...

  4. POJ1321 棋盘问题(简单搜索)

    题意: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放 ...

  5. 解码escape类型的unicode

    content = Regex.Unescape(content);

  6. 金蝶k/3 K3云之家消息查询发送是否成功SQL语句

    金蝶k/3 K3云之家消息查询发送是否成功SQL语句 1是成功,0是还在轮询中未发送,4是发送失败 select * into #tempUserID from ( union select t_Gr ...

  7. 【数据库】MySql分割字符串

    上论坛时看到一个骨骼清奇的分割字符串算法. DROP TABLE IF EXISTS Tmp_AreaCode; CREATE TABLE Tmp_AreaCode( string ) )ENGINE ...

  8. (转)urllib库python2和python3具体区别

    转载链接:https://blog.csdn.net/whatday/article/details/54710403 Python 2 name Python 3 name urllib.urlre ...

  9. APP,H5测试要点

    APP测试重点 一,运行测试 运行过程中,是否有加载提示: 运行速度是否流畅: 各个模块之间的切换是否正常: 二,更新测试:打开旧版app时,是否有更新提示,且在不同的手机版本上都能更新成功:打开新版 ...

  10. python-nmap的函数学习

    简介 python-nmap是一个使用nmap进行端口扫描的python库,它可以很轻易的生成nmap扫描报告,并且可以帮助系统管理员进行自动化扫描任务和生成报告.同时,它也支持nmap脚本输出. 可 ...