public static void main(String[] args) {
Integer i = 10;
Integer j = 10;
System.out.println(i == j); //true Integer a = 128;
Integer b = 128;
System.out.println(a == b); //false int k = 10;
System.out.println(k == i);//true
int kk = 128;
System.out.println(kk == a);//true Integer m = new Integer(10);
Integer n = new Integer(10);
System.out.println(m == n); //false
}

1、Integer 类简介

  首先我们大致看一下Integer是什么,Integer 类在JDK1.0的时候就有了,它是一个类,是 int 基本数据类型的封装类。


  
  基本API如下:

  

  

 

  图片引用:http://blog.csdn.net/litong09282039/article/details/46309541

2、Integer 类和 int 的区别

  ①、Integer 是 int 包装类,final修饰,int 是八大基本数据类型之一(byte,char,short,int,long,float,double,boolean)

  ②、Integer 是类,默认值为null,int是基本数据类型,默认值为0;

  ③、Integer 表示的是对象,用一个引用指向这个对象,而int是基本数据类型,直接存储数值。

3、Integer 的自动拆箱和装箱

  自动拆箱和自动装箱是 JDK1.5 以后才有的功能,也就是java当中众多的语法糖之一,它的执行是在编译期,会根据代码的语法,在生成class文件的时候,决定是否进行拆箱和装箱动作。
  ①、自动装箱

  一般我们创建一个类的时候是通过new关键字,比如:

 Object obj = new Object();

  但是对于 Integer 类,我们却可以这样:

 Integer a = 128;

为什么可以这样,通过反编译工具,我们可以看到,生成的class文件是:

 Integer a = Integer.valueOf(128);

这就是基本数据类型的自动装箱,128是基本数据类型,然后被解析成Integer类。
  ②、自动拆箱

  我们将 Integer 类表示的数据赋值给基本数据类型int,就执行了自动拆箱。

 Integer a = new Integer(128);
int m = a;

  反编译生成的class文件:

 Integer a = new Integer(128);
int m = a.intValue();

  简单来讲:自动装箱就是Integer.valueOf(int i);自动拆箱就是 i.intValue();

4、回顾开头的问题

  我们使用反编译工具Jad,得到的代码如下:

 public static void main(String args[])
{
Integer i = Integer.valueOf(10);
Integer j = Integer.valueOf(10);
System.out.println(i == j);//true
Integer a = Integer.valueOf(128);
Integer b = Integer.valueOf(128);
System.out.println(a == b);//false
int k = 10;
System.out.println(k == i.intValue());//true
int kk = 128;
System.out.println(kk == a.intValue());//true
Integer m = new Integer(10);
Integer n = new Integer(10);
System.out.println(m == n);//false
}

  首先,直接声明Integer i = 10,会自动装箱变为Integer i = Integer.valueOf(10);Integer i 会自动拆箱为 i.intValue()。

  ①、第一个打印结果为 true

  对于 i == j ,我们知道这是两个Integer类,他们比较应该是用equals,这里用==比较的是地址,那么结果肯定为false,但是实际上结果为true,这是为什么?

  我们进入到Integer 类的valueOf()方法:

  

  分析源码我们可以知道在 i >= -128 并且 i <= 127 的时候,第一次声明会将 i 的值放入缓存中,第二次直接取缓存里面的数据,而不是重新创建一个Ingeter 对象。那么第一个打印结果因为 i = 10 在缓存表示范围内,所以为 true。

  ②、第二个打印结果为 false

  从上面的分析我们知道,128是不在-128到127之间的,所以第一次创建对象的时候没有缓存,第二次创建了一个新的Integer对象。故打印结果为false

  ③、第三个打印结果为 true

  Integer 的自动拆箱功能,也就是比较两个基本数据类型,结果当然为true

  ④、第四个打印结果为 true

  解释和第三个一样。int和integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比较。

  ⑤、第五个打印结果为 false

  因为这个虽然值为10,但是我们都是通过 new 关键字来创建的两个对象,是不存在缓存的概念的。两个用new关键字创建的对象用 == 进行比较,结果当然为 false。
回到顶部
5、测试

 Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3; Integer e = 321;
Integer f = 321; Long g = 3L;
Long h = 2L; System.out.println(c == d);//true
System.out.println(e == f);//false
System.out.println(c == (a + b));//true
System.out.println(c.equals((a+b)));//true
System.out.println(g == (a+b));//true
System.out.println(g.equals(a+b));//false
System.out.println(g.equals(a+h));//true

  反编译结果:

  分析:第一个和第二个结果没什么疑问,Integer类在-128到127的缓存问题;

  第三个由于  a+b包含了算术运算,因此会触发自动拆箱过程(会调用intValue方法),==比较符又将左边的自动拆箱,因此它们比较的是数值是否相等。

  第四个对于c.equals(a+b)会先触发自动拆箱过程,再触发自动装箱过程,也就是说a+b,会先各自调用intValue方法,得到了加法运算后的数值之后,便调用Integer.valueOf方法,再进行equals比较。

  第五个对于 g == (a+b),首先计算 a+b,也是先调用各自的intValue方法,得到数值之后,由于前面的g是Long类型的,也会自动拆箱为long,==运算符能将隐含的将小范围的数据类型转换为大范围的数据类型,也就是int会被转换成long类型,两个long类型的数值进行比较。

  第六个对于 g.equals(a+b),同理a+b会先自动拆箱,然后将结果自动装箱,需要说明的是 equals 运算符不会进行类型转换。所以是Long.equals(Integer),结果当然是false

  第七个对于g.equals(a+h),运算符+会进行类型转换,a+h各自拆箱之后是int+long,结果是long,然后long进行自动装箱为Long,两个Long进行equals判断。
————————————————
版权声明:本文为CSDN博主「郑州尚学堂李老师」的原创文章,遵循undefined版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/teacher_lee_zzsxt/article/details/79230501

Integer 类和 int 基本数据类型的区别的更多相关文章

  1. Integer类toString(int i,int radix)方法

    Integer类toString(int i,int radix)方法: 首先抛出java的api中的介绍: public static String toString(int i, int radi ...

  2. Integer 类和 int 的区别

    http://www.cnblogs.com/ysocean/p/8075676.html public static void main(String[] args) {     Integer i ...

  3. Integer类的parseInt和valueOf的区别

    我们平时应该都用过或者见过parseInt和valueOf这两个方法.一般我们是想把String类型的字符数字转成int类型.从这个功能层面来说,这两个方法都一样,都可以胜任这个功能. 但是,我们进入 ...

  4. JDK1.8源码(二)——java.lang.Integer 类

    上一篇博客我们介绍了 java.lang 包下的 Object 类,那么本篇博客接着介绍该包下的另一个类 Integer.在前面 浅谈 Integer 类 博客中我们主要介绍了 Integer 类 和 ...

  5. java中的BigDecimal和String的相互转换,int和String的类型转换,Integer类和String相互转换

    一: /*由数字字符串构造BigDecimal的方法 *设置BigDecimal的小数位数的方法 */ 注:BigDecimal在数据库中存的是number类型. import java.math.B ...

  6. 浅谈 Integer 类

    在讲解 Integer 之前,我们先看下面这段代码: public static void main(String[] args) { Integer i = 10; Integer j = 10; ...

  7. Integer类入门学习

    Integer类 它是一个类,是 int 基本数据类型的封装类. 基本API Integer 类和 int 的区别 Integer 是 int 包装类,int 是八大基本数据类型之一(byte,sho ...

  8. 【转】浅谈 Integer 类

    突然发现自己对Integer i = 10;这种语法不太明白,于是乎有了这篇文章,那么在讲解 Integer 之前,我们先看下面这段代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

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

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

随机推荐

  1. centos做免密登录

    CentOS 6.9 SSH配置用户免密码登录 1. 演示环境: 192.168.1.144:CentOS 6.9 x86_64 192.168.1.146:CentOS 7.4 x86_64 2.  ...

  2. Laravel 开发环境搭建

    本人使用的是Laravel5.5版本,需要PHP7支持,所以安装的环境是Apache2.php7.0.mysql5.7,系统为ubuntu14.04LTS(14以下的版本对php7支持不够),主要参考 ...

  3. php的字符串{}选定与{变量}

    $str = "abcdefg"; echo $str{2};//输出c $a = "test"; echo "ddd{$a}";//输出d ...

  4. 凑出和相等的k组数,玄学结论——hdu6616

    [1,n]n个数分成k组,每组n/k个,问k组数和相等的解决方案 首先(1+n)*n/2判定一下是否可以被k整除 n/k为偶数时显然成立 n/k为奇数时每组数前三个很难配,我想了一种玄学的结论,也证明 ...

  5. 字符串hash+找模数——cf985F

    19260817比自然溢出都要好使 /* 把原串变成用26个01串表示,第i个串对应的字符是i 然后进行字符串hash,s和t双射的条件是26个串的hash值排序后一一相等 */ #include&l ...

  6. C语言-实例3个数由小到大排序

    VS2012 //C语言实例 3个数由小到大排序 #include <stdio.h> void main() { int a, b, c, t; printf("Please ...

  7. Greenplum(PostgreSql)函数实现批量删除表

    项目做库迁移,前期需要经常调整表结构语句,涉及多次的批量drop,本着偷懒精神写了这个函数.鉴于本函数在生产环境有巨大风险,建议测试完毕后立即删除. 主要步骤很简单:1)从pg_tables查询得到相 ...

  8. BBB 常用指令

    source .bashrc root@beaglebone:~# route add default gw 192.168.7.1 echo BB-SPIDEV0 > /sys/devices ...

  9. arr = map(float,arr)输出问题

    代码: arr = ['22','44','66','88']arr = map(float,arr)print(arr) 输出: <map object at 0x000001B48C30EE ...

  10. solr 启动报错Cannot load analyzer: org.wltea.analyzer.lucene.IKAnalyzer

    schema.xml 配置文件信息: <field name="title" type="text_ik" indexed="true" ...