Integer封装与拆箱

简介:

目录:

  1. Integer自动封装的陷阱
  2. Integer自动拆箱机制

Integer自动封装的陷阱

public class IntegerDemo {

public static void main(String[] args) {

    Integer a=1000,b=1000;
    Integer c=100,d=100; 

    System.out.println(a==b);//false
    System.out.println(c==d);//true
  }

}

我们知道==运用于对象时,比较的是双方是否拥有同一个对象,而非简单的比较双方是否拥有相同的值,这里的abcd都是新建出来的对象,按理说都应该输入false才对。

但是道理其实很简单,我们去看下Integer这个类的源码就明白了其中的奥秘:

public static Integer valueOf(int i) {
    assert IntegerCache.high >= 127;
    //IntegerCache.low = -128, IntegerCache.high = 127
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

当我们声明一个Integer c = 100;的时候。此时会进行自动装箱操作,简单点说,也就是把基本数据类型转换成Integer对象,而转换成Integer对象正是调用的valueOf方法,可以看到,Integer中把-128至127 缓存了下来。官方解释是小的数字使用的频率比较高,所以为了优化性能,把这之间的数缓存了下来。这就是为什么这道题的答案回事false和ture了。当声明的Integer对象的值在-128-127之间的时候,引用的是同一个对象,所以结果是true。

Integer自动拆箱机制

public class IntegerDemo {

public static void main(String[] args) {

    Integer a = new Integer(1000);
    int b = 1000;
    Integer c = new Integer(10);
    Integer d = new Integer(10);

    System.out.println(a == b);//true
    System.out.println(c == d);//false

  }
}

看到这个答案很多小伙伴又会不解,先来说下第二个,Integer不是把-128至127缓存起来了吗?这不是应该是true嘛,但是你仔细看,这里的Integer是我们自己new出来的,并不是用的缓存,所以结果是false。 现在来看第一个为啥又是true了呢? 首先这里的值为1000,肯定和我们所知的Integer缓存没有关系。既然和缓存没有关系,a是新new出来的对象,按理说输入应该是false才对。但是注意b这里是int类型。当int和Integer进行==比较的时候,Java会把Integer进行自动拆箱,也就是把Integer转成int类型,所以这里进行比较的是int类型的值,所以结果即为true。

Integer封装与拆箱的更多相关文章

  1. java Integer类以及拆箱和装箱

    package com.ilaw.boson.controller; public class Demo { public static void main(String[] args) { Inte ...

  2. int和Integer的自动拆箱/装箱相关问题

    java中为没一种基本类型都提供相应的包装类型. byte,short,char,int,long,float,double和boolean Byte,Short,Character,Integer, ...

  3. Integer自动装箱拆箱bug,创建对象在-128到127

    1 public class Demo3 { public static void main(String[] args) { Integer a = 1; Integer b = 2; Intege ...

  4. Integer的自动拆箱

    public class Test2{ public static void main(String[] args){ Integer a=1; Integer b=2; Integer c=3; I ...

  5. Integer自动装拆箱

    public static void main(String[] args) { Integer a1 = 1; Integer a2 = 1; Integer b1 = 127; Integer b ...

  6. int和Integer及拆箱与装箱

    int和Integer 如果面试官问Integer与int的区别:估计大多数人只会说道两点,Ingeter是int的包装类,int的初值为0,Ingeter的初值为null.但是如果面试官再问一下In ...

  7. Integer装箱拆箱、参数传递

    拆箱装箱 举个例子 @Test public void testEquals() { int int1 = 12; int int2 = 12; Integer integer1 = new Inte ...

  8. 【转】java 自动装箱与拆箱

    java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...

  9. Java自动装箱和拆箱

    jdk5.0之后,在基本数据类型封装类之间增加了自动装箱和拆箱的功能,其实“自动”的实现很简单,只是将装箱和拆箱通过编译器,进行了“自动补全”,省去了开发者的手动操作. 而进行封装类与对应基本数据类型 ...

随机推荐

  1. word中设置前几页为罗马数字,后几页设置为阿拉伯数字

    假如第1-5页摘要部分页脚要是罗马数字,第6页开始是正文部分是阿拉伯数字,起始页为1. WORD2003 1.将光标定位在第5页末尾处,在菜单栏中依次点击“插入——分隔符——(分节符类型)下一页”.按 ...

  2. c#播放器

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. Sumdiv(快速幂+约数和)

    Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16244 Accepted: 4044 Description C ...

  4. nohup & rabbitmq & python

    用Python脚本执行rabbitmq的消费 nohup python consumer.py > out.log & 结果郁闷啊,怎么都查看不到输出! 终于找到了答案: 原来pytho ...

  5. Unix网络编程--卷二:进程间通信

    Unix网络编程--卷二:进程间通信 本书是一部Unix网络编程的经典之作!进程间通信(IPC)几乎是所有Unix程序性能的关键,理解IPC也是理解如何开发不同主机网络应用程序的必要条件.本书从对Po ...

  6. 链表——PowerShell版

    链表是由一系列节点串连起来组成的,每一个节点包括数值部分和指针部分,上一节点的指针部分指向下一节点的数值部分所在的位置. 在C语言中我们有两种方式来定义链表—— 1.定义结构体:来表示链表中的节点,节 ...

  7. Reflection实现通用增删改

    新增 /// <summary> /// 通用新增方法 /// </summary> /// <param name="arr">一行数据封装的 ...

  8. 北邀 E Elegant String

    E. Elegant String Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB   64-bit integer ...

  9. 提高开发效率的 Eclipse 实用操作

    工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟后,不用鼠标,便可进行编程开发,避免鼠标分 ...

  10. Codeforces Round #368 (Div. 2) B

    Description Masha wants to open her own bakery and bake muffins in one of the n cities numbered from ...