好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java 详解包装类Integer与int有哪些共通和不同

1、包装类型是什么?

Java 为每一个基本数据类型都引入了对应的包装类型, int 的包装类就是 Integer ,从 Java 5 开始引入了自动装箱/拆箱机制,把基本类型转换成包装类型的过程叫做装箱;反之,把包装类型转换成基本类型的过程叫做拆箱,使得二者可以相互转换。

Java 为每个原始类型提供了包装类型:

原始类型: boolean,char,byte,short,int,long,float,double 包装类型: Boolean,Character,Byte,Short,Integer,Long,Float,Double

2、基本类型和包装类型有什么区别?

包装类型可以为 null,而基本类型不可以。 包装类型可用于泛型,而基本类型不可以。 泛型不能使用基本类型,因为使用基本类型时会编译出错。因为泛型在编译时会进行类型擦除,最后只保留原始类型,而原始类型只能是 Object 类及其子类——基本类型是个特例。

?

1

2

List< int > list = new ArrayList<>(); // 提示 Syntax error, insert "Dimensions" to complete ReferenceType

List<Integer> list = new ArrayList<>();

基本类型比包装类型更高效。 基本类型在栈中直接存储的具体数值,而包装类型则存储的是堆中的引用。 很显然,相比较于基本类型而言,包装类型需要占用更多的内存空间。

3、解释一下自动装箱和自动拆箱?

在讲解自动装箱和自动拆箱之前,我们先来认识一下 Integer.valueOf() 和 IntegerCache 方法。

Integer.valueOf()

?

1

2

3

public static Integer valueOf(String s, int radix) throws NumberFormatException {

         return Integer.valueOf(parseInt(s,radix));

  }

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/**

  * (1)在-128~127之内:静态常量池中cache数组是static final类型,cache数组对象会被存储于静态常量池中。

  * cache数组里面的元素却不是static final类型,而是cache[k] = new Integer(j++),

  * 那么这些元素是存储于堆中,只是cache数组对象存储的是指向了堆中的Integer对象(引用地址)

  *

  * (2)在-128~127 之外:新建一个 Integer对象,并返回。

  */

public static Integer valueOf( int i) {

     assert IntegerCache.high >= 127 ;

     if (i >= IntegerCache.low && i <= IntegerCache.high) {

         return IntegerCache.cache[i + (-IntegerCache.low)];

     }

     return new Integer(i);

}

调用 Integer.valueOf() 方法,也就是 如果数值在 -128~127 的话,那么直接返回静态常量池中的 cache 数组对象,否则就调用 new Integer() 进行创建对象。

IntegerCache

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

/**

   * 缓存支持自动装箱的对象标识语义 -128和127(含)。

   * 缓存在第一次使用时初始化。 缓存的大小可以由-XX:AutoBoxCacheMax = <size>选项控制。

   * 在VM初始化期间,java.lang.Integer.IntegerCache.high属性可以设置并保存在私有系统属性中

  */

private static class IntegerCache {

     static final int low = - 128 ;

     static final int high;

     static final Integer cache[];

 

     static {

         // high value may be configured by property

         int h = 127 ;

         String integerCacheHighPropValue =

             sun.misc.VM.getSavedProperty( "java.lang.Integer.IntegerCache.high" );

         if (integerCacheHighPropValue != null ) {

             int i = parseInt(integerCacheHighPropValue);

             i = Math.max(i, 127 );

             // Maximum array size is Integer.MAX_VALUE

             h = Math.min(i, Integer.MAX_VALUE - (-low) - 1 );

         }

         high = h;

 

         cache = new Integer[(high - low) + 1 ];

         int j = low;

         for ( int k = 0 ; k < cache.length; k++) {

             cache[k] = new Integer(j++); // 创建一个对象

         }

     }

 

     private IntegerCache() {}

}

自动装箱:将基本数据类型重新转化为对象

?

1

2

3

4

5

6

public class Test { 

     public static void main(String[] args) { 

         // 声明一个Integer对象,用到了自动的装箱:解析为:Integer num = Integer.valueOf(9);

      Integer num = 9 ;

     } 

9是属于 int 基本数据类型的,原则上它是不能直接赋值给一个对象 Integer 的。但jdk1.5 开始引入了自动装箱/拆箱机制,就可以进行这样的声明,自动将基本数据类型转化为对应的封装类型,成为一个对象以后就可以调用对象所声明的所有的方法。

自动拆箱:将对象重新转化为基本数据类型

?

1

2

3

4

5

6

7

8

9

public class Test { 

      public static void main(String[] args) { 

          / /声明一个Integer对象

       Integer num = 9 ;

         

          // 进行计算时隐含的有自动拆箱

       System.out.print(num--);

      } 

  } 

因为对象时不能直接进行运算的,而是要转化为基本数据类型后才能进行加减乘除。

4、int 和 Integer 有什么区别?

Integer是int的包装类;int是基本数据类型; Integer变量必须实例化后才能使用;int变量不需要; Integer实际是对象的引用,指向此new的Integer对象;int是直接存储数据值 ; Integer的默认值是null;int的默认值是0。

5、两个new生成的Integer变量的对比

?

1

2

3

Integer i = new Integer( 10000 );

Integer j = new Integer( 10000 );

System.out.print(i == j); //false

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

6、Integer变量和int变量的对比

?

1

2

3

4

5

int a = 10000 ;

Integer b = new Integer( 10000 );

Integer c= 10000 ;

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

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

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

7、非new生成的Integer变量和new Integer()生成变量的对比

?

1

2

3

Integer b = new Integer( 10000 );

Integer c= 10000 ;

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

非new生成的Integer变量和new Integer()生成的变量比较时,结果为false。(因为 Integer c=10000 等价于 Integer.valueOf(10000) ,如果其中的数值在 -128 ~ 127 之间的话,那么 c 指向的就是静态常量池中的对象,否则就是指向堆中所生成的对象。而 new Integer() 所生成的对象位于堆中,所以结果一定为 false )

8、两个非new生成的Integer对象的对比

?

1

2

3

4

5

6

7

Integer i = 100 ;

Integer j = 100 ;

System.out.print(i == j); //true

 

Integer l = 128 ;

Integer r = 128 ;

System.out.print(l == r); //false

因为 Integer i =100 等价于 Integer.valueOf(100) ,由于其中的数值在 -128 ~ 127 之间,那么 i 和 j 指向的都是静态常量池中的对象,所以是同一个对象,所以结果为 true 。

因为 Integer l =128 等价于 Integer.valueOf(128) ,由于其中的数值在 -128 ~ 127 之外,那么 l 和 r 指向的都是其各自在堆中生成的对象,所以是不同的对象,所以结果为 false 。

到此这篇关于Java 详解包装类Integer与int有哪些共通和不同的文章就介绍到这了,更多相关Java 包装类Integer内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_56727438/article/details/123537841

查看更多关于Java 详解包装类Integer与int有哪些共通和不同的详细内容...

  阅读:14次