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;}

Integer和int使用==比较的总结的更多相关文章

  1. Integer与int的区别(包装类和基本数据类型的区别)

    1. 默认值 int默认值为0,Integer的默认值为null.推论:Integer既可以表示null又可以表示0 2. 包装类中提供了该类型相关的很多算法操作方法 如把十进制装换为2进制(toBi ...

  2. Integer与int的区别

    简述:int与Integer的区别: 对于它们,我们可能只是知道简单的区别.Integer是int的一个封装类,int的初始值为0,而Integer的初始值为null.但是他们之间真的仅仅只有这些区别 ...

  3. Integer与int的种种比较

    package com.lxm.basics; public class IntegerTest { public static void main(String[] args) { Integer ...

  4. Integer与int的种种比较你知道多少?

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

  5. java Integer和int的拆箱与装箱

    官网:http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 1.赋值: a. 把int类型赋值给Integer类型:JVM会 ...

  6. Java数据类型中String、Integer、int相互间的转换

    1.Integer转换成int的方法 Integer i;  int k = i.intValue(); 即Integer.intValue(); 2.int转换成Integer int i; Int ...

  7. Integer和int的详细比较(转)

    Integer与int的区别我们耳熟详的有两点:1.Integer是int的包装类.2.Integer的默认初始值是null,而int的默认初试值是0. 下面通过代码进行详细比较. public cl ...

  8. Integer.valueOf(int)及自动装箱内幕

    Integer为什么要提供功能与new Integer(xx)一样的valueOf(xx)方法呢,看了源代码之后,我发现了惊人的内幕. public static Integer valueOf(in ...

  9. 关于Integer与int

    integer a=new integer(1); integer b=new integer(1); int c=1; integer d=1; a==b  false因为地址不同: a==c  t ...

  10. integer与int区别以及integer.values()方法详解

    声明:本文为博主转载文章,原文地址见文末. 知识点1:integer和int的区别 /* * int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为 ...

随机推荐

  1. node+mongoDB+express项目需求解释

    1. morgon模块 --- morgon 用于打印日志,分别为向后台打印和向文件中打印两种情况.stackoverflow. 2. app.use(bodyParser.json()) 3. de ...

  2. pat1082. Read Number in Chinese (25)

    1082. Read Number in Chinese (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  3. 上传文件,使用FormData进行Ajax请求,jsoncallback跨域

    通过传统的form表单提交的方式上传文件: <form id= "uploadForm" action= "http://localhost:8080/cfJAX_ ...

  4. 从零开始的全栈工程师——js篇2.18(js的运动)

    一.元素的 client offset scroll 三个系列 clientWidth / clientHeight / clientTop / clientLeftoffsetWidth / off ...

  5. Android开发之——依赖冲突Program type already present

    前言 实际开发中,为了提高开发速度和效率,总避免不了引用第三方提供的依赖和类库,如果含有相同依赖的类库被我们引用时,而他们的版本又不相同,就有可能会导致一系列问题和异常,本文结合本人时间总结和他人经验 ...

  6. Android基础Activity篇——其他隐式Intent

    1.使用隐式Intent调用浏览器 修改FirstActivity中的按钮点击事件代码. Intent intent=new Intent(Intent.ACTION_VIEW); intent.se ...

  7. Java学习笔记——集合

    类集简介 从JDK1.2开始Java引入了类集开发框架,所谓的类集指的就是一套动态对象数组的实现方案,在实际开发之中没有有何一项开发可以离开数组,但是传统的数组实现起来非常的繁琐.而且长度是其致命伤, ...

  8. Centos 6/RHEL disable the IPv6 module.

    http://minimallinux.blogspot.com/2013/07/centos-6rhel-disable-ipv6-module.html IPv6 was introduced t ...

  9. ODBC驱动程序丢失解决方法

    今天运行SqlDbx连接数据库的时候报错,提示没有找到相应的ODBC driver,打开ODBC管理面板一看,发现里面的驱动程序都不见了.这时想起今天卸载了一个成本核算软件后成这样的,网上搜索一下只需 ...

  10. 使用selenium grid的hub做分发,且可查看分发后的服务器IP地址

    背景:借助selenium 的grid做分布式运行,进行分发任务,(目前不做多浏览器的操作,只对谷歌浏览器进行操作) 目前在A服务器(http://10.40.6.24:4444)上注册了一个hub, ...