基本概念的区分:

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

Integer、new 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){
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:

   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

Java中Integer 和 int的区别的更多相关文章

  1. java中Integer和int的区别(转)

    int和Integer的区别 1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的引 ...

  2. java中Integer和int的区别

    亲看这里 例子: public class Test { public static void main(String[] args) { Integer i = new Integer(128); ...

  3. Java|从Integer和int的区别认识包装类

    https://blog.csdn.net/darlingwood2013/article/details/96969339?utm_medium=distribute.pc_relevant.non ...

  4. java中Integer与int装箱拆箱一点收获

    示例代码: class BoxIntInteger { public static void main(String[] args) { Integer a = new Integer(10111); ...

  5. Java中Integer和int的异同

    public void Test1() { int a = 128; Integer b = 128; Integer c = 128; //Integer会自动拆箱成int,所以为ture Syst ...

  6. Java中Integer与int对比的一些坑

    Integer与int类型的关系 Integer是int的包装类,int的默认值是0,而Integer的默认值是null(我们经常在代码中使用的Integer.valueOf() 和xx.intVal ...

  7. java POJO中 Integer 和 int 的不同,用int还是用Integer

    https://www.jianshu.com/p/ff535284916f [int和Integer的区别] int是java提供的8种原始类型之一,java为每个原始类型提供了封装类,Intege ...

  8. Java中Long与long的区别(转)

    Java中Long与long的区别(转) [本文转载自:http://www.cnblogs.com/bluestorm/archive/2012/04/22/2464739.html] 转载请联系原 ...

  9. java中堆和堆栈的区别

    java中堆和堆栈的区别(一) 1.栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. 2. 栈的优势是,存取 ...

随机推荐

  1. Django_环境配置(一)

    一.安装Django # 在CMD中运行 pip install django # 查看djangp版本 python manage.py version 二.创建项目 # 在CMD中运行django ...

  2. vue3.0 没有 vue.condig.js 解决

    第一次用 vue3.0 ,发现没有vue.config.js  ,只有一个babel.config.js 怎么办? 需要在根目录手动添加一个即可,如下 相关的配置说明 module.exports = ...

  3. Shell统计每个单词出现的个数

    题目链接 题目描述 写一个 bash脚本以统计一个文本文件 nowcoder.txt 中每个单词出现的个数. 为了简单起见,你可以假设: nowcoder.txt只包括小写字母和空格. 每个单词只由小 ...

  4. 虚拟机上CentOS7环境配置

    原文链接:https://www.toutiao.com/i6493449649939022350/ 之前网络和基本环境以及完成,现在我们再配置一些内容,方便我们之后的学习. 设置网络YUM源 下载我 ...

  5. mybatis(1.2)

    为什么执行sql语句后 数据库表中不会更新 需要我们手动配置  两种方法 如下: 1:调用SqlSession接口的commit方法 2:获取Session的时候  SqlSessionFactory ...

  6. 重大升级!灵雀云发布全栈云原生开放平台ACP 3.0

    云原生技术的发展正在改变全球软件业的格局,随着云原生技术生态体系的日趋完善,灵雀云的云原生平台也进入了成熟阶段.近日,灵雀云发布重大产品升级,推出全栈云原生开放平台ACP 3.0.作为面向企业级用户的 ...

  7. RHCSA 第八天

    1.查询ip的几种方式: ip, ifconfig, nmcli,nmtui 2.nmcli命令使用: a.在ens160网卡上新建连接static_con,并配置静态ip b.在ens160网卡上新 ...

  8. Message deduplication 这里的去重与你想的可能不一样|Apache Pulsar 技术系列

    导语 Apache Pulsar 是一个多租户.高性能的服务间消息传输解决方案,支持多租户.低延时.读写分离.跨地域复制.快速扩容.灵活容错等特性.腾讯云内部 Pulsar工作组对 Pulsar 做了 ...

  9. pytest文档2-用例执行

    用例设计原则 1.文件名以test_******.py文件和*******_test.py 2.以test_****开头的函数 3.以Test***开头的类 4.以test_*****开头的方法 5. ...

  10. golang中goroutine池的使用

    1. 概念本质上是生产者.消费者模型可以有效的控制goroutine数量,防止暴涨案例:生成一个随机数,计算该随机数每一个数字相加的和,例如:123:1+2+3=6主协程负责生产数据发送到待处理通道中 ...