一、由于Integer变量实际上是对一个Integer对象的引用,所以两个通过new生成的Integer变量永远是不相等的(因为new生成的是两个对象,其内存地址不同)。

例子1:

Integer a5 = new Integer(-128);
Integer a6 = new Integer(-128);
System.out.println(a5 == a6);

运行结果:

二、Integer变量和int变量比较时,只要两个变量的值是向等的,则结果为true(因为包装类Integer和基本数据类型int比较时,java会自动将Integer拆箱为int,然后进行比较,实际上就变为两个int变量的比较)

举例2:

int a8 = -129;
Integer a9 = new Integer(-129);
System.out.println(a8 == a9);

运行结果:

三、非new生成的Integer变量和new Integer()生成的变量比较时,结果为false。(因为非new生成的Integer变量指向的是java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同)

举例3:

Integer num1 = -128;
Integer num2 = new Integer(-128);
System.out.println(num1 == num2);

运行结果:

四、对于两个非new生成的Integer对象,进行比较时,如果两个变量的值在区间-128到127之间,则比较结果为true,如果两个变量的值不在此区间,则比较结果为false

举例4:

Integer a1 = -128;
Integer a2 = -128;
System.out.println(a1 == a2);
System.out.println("=============");
Integer a3 = -129;
Integer a4 = -129;
System.out.println(a3 == a4);

运行结果:

原理分析:

Integer i1 = 20 或者Integer i1 = 200的时候,会调用 Integer中的 valueOf()方法。我们看一下这个方法的源码:

 /*** Returns an {@code Integer} instance representing the specified* {@code int} value.  If a new {@code Integer} instance is not* required, this method should generally be used in preference to* the constructor {@link #Integer(int)}, as this method is likely* to yield significantly better space and time performance by* caching frequently requested values.** This method will always cache values in the range -128 to 127,* inclusive, and may cache other values outside of this range.** @param  i an {@code int} value.* @return an {@code Integer} instance representing {@code i}.* @since  1.5*/public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}

这段代码的作用是把int类型转换成Integer,也就是装箱,它返回一个 Integer对象。
这里又涉及到IntegerCache,对于这段静态代码(这里JDK版本是JDK1.8):

 /*** Cache to support the object identity semantics of autoboxing for values between* -128 and 127 (inclusive) as required by JLS.** The cache is initialized on first usage.  The size of the cache* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.* During VM initialization, java.lang.Integer.IntegerCache.high property* may be set and saved in the private system properties in the* sun.misc.VM class.*/private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint h = 127;String integerCacheHighPropValue =sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");if (integerCacheHighPropValue != null) {try {int i = parseInt(integerCacheHighPropValue);i = Math.max(i, 127);// Maximum array size is Integer.MAX_VALUEh = Math.min(i, Integer.MAX_VALUE - (-low) -1);} catch( NumberFormatException nfe) {// If the property cannot be parsed into an int, ignore it.}}high = h;cache = new Integer[(high - low) + 1];int j = low;for(int k = 0; k < cache.length; k++)cache[k] = new Integer(j++);// range [-128, 127] must be interned (JLS7 5.1.7)assert IntegerCache.high >= 127;}private IntegerCache() {}}

这段代码的作用是,在Integer类装载入内存时,把[-128, 127]范围内的整型数据装包成Integer类,并将其对应的引用放入到cache数组中。

从上面的源码可以看出,valueOf()在返回之前,会进行判断,判断当前 i的值是否在 -128到127之间。
如果存在,则直接返回引用,不再重新开辟内存空间。
如果不存在,就创建一个新的对象。
利用缓存,这样做既能提高程序执行效率,还能节约内存。

Integer i1 = 20; Integer i2 = 20; 因为 IntegerCache中已经存在此对象,直接返回引用,引用相等并且都指向缓存中的数据,所以这时候i1 == i2返回true。

Integer i1 = 200; Integer i2 = 200;因为i1,i2的值大于127,不在[-128, 127]范围内,所以虚拟机会在堆中重新new一个 Integer对象来存放200,创建两个对象就会产生两个这样的空间。两个空间的地址不同,返回到栈中的引用的值也就不同,所以这时候i1 == i2返回false。

参考文章: Java中Integer.valueOf()解读_shboli的博客-CSDN博客_integer.valueof()作用

int类型和Integer类型数据的比较相关推荐

  1. oracle的int范围,oracle中int类型和number类型区别

    oracle中int类型和number类型区别 INT类型是NUMBER类型的子类型. 下面简要说明: (1)NUMBER(P,S) 该数据类型用于定义数字类型的数据,其中P表示数字的总位数(最大字节 ...

  2. (Redis_学习一)Redis关于string类型和hash类型数据操作

    Redis关于string类型和hash类型数据操作 set name xiaohongyang // get name setnx name xiaohy get name setex name 1 ...

  3. int类型和byte类型的强制类型转换

    今天在读<Java网络编程>这本书的第二章 流 时,看到书中有一个地方关于int强制转换为byte类型时应注意的地方.这个地方有点细节,不过就应该把这种细节把握住. 情况是这样的,讲到In ...

  4. C语言int类型和float浮点型数据在内存中的存储方式

    int 类型在内存中占4个字节,而一个字节是8个比特位,所以int类型占32个比特位. float类型在内存中同样也占4个字节,所以其也是占32个比特位. 一个比特位就是一个0或1,所以其在二进制位数 ...

  5. int类型和Integer在hibernate影射时的区别

    2019独角兽企业重金招聘Python工程师标准>>> int是java的基本的数据类型 integer是int的包装类 在有的时候,比如集合只能放类而不能放基本的数据类型,这时候就 ...

  6. MongoDB查询时排序字段为int类型和string类型的区别

    最近在开发中遇到一个mongo查询排序的问题,项目中一些高频访问的热数据是放在mongoDB里的,mongo支持很多像SQL一样的操作比如sort就对应于SQL的order by. 比方我们有一个集合 ...

  7. int类型和number类型区别

    一句话:INT类型是NUMBER类型的子类型. 下面简要说明: (1)NUMBER(P,S) 该数据类型用于定义数字类型的数据,其中P表示数字的总位数(最大字节个数),而S则表示小数点后面的位数.假设 ...

  8. [转载] Python中int类型和string类型的相互转换

    参考链接: Python类型转换和类型转换 1.字符串转换成int a = '10' int(a) //十进制string转化为int, 10int(a, 16) //十六进制string转化为int ...

  9. Java学习(4)—— 布尔类型、基本数据类型转换、基本数据类型和String类型的转换

    布尔类型 也叫Boolean类型,boolean只允许取true和false. boolean占一个字节. 适用于逻辑运算,流程控制语句. 有默认类型,是false.但是应该声明为成员变量或是静态变量 ...

最新文章

  1. jgroup 概述--官方文档
  2. matlab选择结构
  3. 利用WDS实现零接触部署Windows 7 VHD
  4. DESUtils 加解密时 Given final block not properly padded bug小记
  5. 如何用php查不同,php-MySql调查不同查询
  6. pb string 接收dll按值返回_JavaScript 是如何工作的:JavaScript 的共享传递和按值传递...
  7. WinCE下音频频谱显示效果图
  8. MTK之UART串口收发数据
  9. UVA10946 You want what filled?【DFS】
  10. Asp.net中的页面乱码的问题
  11. Java Jsoup爬虫入门
  12. Git版本控制管理(七)--提交和查看提交历史
  13. jq怎么获取值与下拉框怎么获取值
  14. 超市网店营销与接口测试
  15. tableau的骑行路线地理数据可视化
  16. 变异检测:vcf文件合并
  17. android 手机 报证书错误,Android设备中的SSL证书错误
  18. oracle付款汇兑损益怎么产生,月末汇兑损益怎么算调整分录如何做
  19. python太极代码_Python : Turtle绘图-太极图(代码)
  20. 笔记本电脑开机到登入页面扩展显示器和电脑突然黑屏很久才显示

热门文章

  1. 你羡慕我国庆能出去旅游,我羡慕你加班3倍工资
  2. rrpp协议如何修改_华为交换机—RRPP协议
  3. 物联网协议-CoAP协议简介
  4. 如何为你的网站植入广告,赚取收益呢GoogleAdsense首选
  5. 上手阿里云服务器(一)——搭建LAMP开发环境、防火墙、文件传输
  6. ionic ActionSheet(上拉菜单)
  7. 最通俗易懂——TCP/IP协议,让你明明白白!
  8. 经典数据结构之2-3树
  9. linux打开文件脚本,linux脚本文件实现的功能有哪些剧本用工具打开
  10. 刷主板bios改变机器码_主板BIOS升级超完整教程,一学就会!