public static void main(String[] args) {
int i1 = 128;
Integer i2 = 128;
Integer i3 = new Integer(128);
System.out.println(i1 == i2);//true
System.out.println(i1 == i3);//true
System.out.println("**************************************");
Integer i4 = 127;
Integer i5 = 127;
Integer i6 = 128;
Integer i7 = 128;
System.out.println(i4 == i5);//true
System.out.println(i6 == i7);//false
System.out.println("**************************************");
Integer i8 = new Integer(127);
Integer i9 = new Integer(127);
System.out.println(i8 == i9);//false
System.out.println(i8.equals(i9));//true
System.out.println(i4 == i8);//false
/* Output:
true
true
**************************************
true
false
**************************************
false
true
false
*/
}
  1. 第5和第6行的结果都为true。因为Integer与int比较时,Ingeger都会自动拆箱(jdk1.5以上)。
  2. 第12行结果为true,第13行结果为false。
    因为Java在编译的时候,Integer i4=127被翻译成-> Integer i4= Integer.valueOf(127);
    JDK源码:
    /**
    * 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);
    }

    看一下源码大家就会明白,对于-128到127之间的数,会进行缓存,Integer i6 = 127时,会将127进行缓存,下次再写Integer i7 = 127时,就会直接从缓存中取,就不会new了。

  3. i8、i9使用的是new, 对象不一样,所以第17行结果为false,第18行结果为true ,第19行结果为false。

总结

  1. Ingeter是int的包装类,int的初值为0,Ingeter的初值为null。
  2. 无论如何,Integer与new Integer()不会相等。不会经历拆箱过程,i8的引用指向堆,而i4指向专门存放他的内存(常量池),他们的内存地址不一样,使用 == 比较都为false。
  3. 两个都是非new出来的Integer,使用 == 比较,如果数在-128到127之间,则是true,否则为false
  4. 两个都是new出来的,==比较都为false。若要比较值是否相等,需使用equals方法进行比较。
  5. int和Integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比。

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}

最新文章

  1. WPF这样的界面应该如何编写呢?
  2. 【DWR系列02】-DWR逆向Ajax即服务器推送
  3. Mysql 5.7 使用SSL安全连接
  4. 发布 PM2.5 数据的城市列表
  5. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
  6. javascript 与和非
  7. MVC 强类型视图
  8. Oracle基础 (十四)其他函数
  9. UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)
  10. [转]单例模式与静态变量在PHP中
  11. LeetCode 6. ZigZag Conversion Question
  12. Linux企业级项目实践之网络爬虫(13)——处理user-agent
  13. C# Socket 简易的图片传输
  14. IE添加信任站点并设置允许ActiveX控件的VBS脚本
  15. Git学习 -- 工作区和暂存区
  16. 先从_proto_下手理解原型--原型学习(一)
  17. 深入理解redis数据类型
  18. &lt;转&gt;jmeter(十二)关联之正则表达式提取器
  19. 文件操作 day8
  20. 解决ubuntu安装系统默认没有创建root用户

热门文章

  1. [Silverlight]调用外部可执行程序
  2. javascript典型实例
  3. stm32 窗口看门狗学习(一)
  4. netty之==TCP粘包/拆包问题解决之道(一)
  5. Xtrareport 多栏报表
  6. 在Centos7中安装Docker并实例化Mysql
  7. InvocationTargetException异常
  8. Github站点搭建 gh-pages
  9. Android 仿微信朋友圈拍小视频上传到服务器
  10. python的元组