暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

JAVA包装类介绍

Hello 帅帅 2021-02-05
1156

一、包装类的继承关系

二、包装类的自动装箱

在应用中我们经常需要进行基本类型数据和包装类对象之间的互转。

    //可以直接赋值
    Integer i1 = 123; //自动装箱调用的是Integer.valueOf(123);
    int i2=i1; //自动拆箱调用的是i1.intValue();

    三、包装类的缓存

    代码:

      public static void main(String[] args) {
      Integer i1 = 1;
      Integer i2 = 1;
      Integer i3 = Integer.valueOf(2);
      Integer i4 = new Integer(2);
      Integer i5 = 200;
      Integer i6 = 200;
      System.out.println((i1 == i2)); // true
      System.out.println(i1.equals(i2)); // true
      System.out.println((i3 == i4)); // false
      System.out.println(i3.equals(i4)); // true
      System.out.println((i5 == i6)); // false
      System.out.println(i5.equals(i6)); // true
      }

      之所以会出现上述情况我们可以点开valueOf()源码

            @HotSpotIntrinsicCandidate
        public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
        }

        可以看出在(i >= IntegerCache.low && i <= IntegerCache.high)
        时不创建新的对象

        再点开IntegerCache
        源代码

          private static class IntegerCache {
          static final int low = -128;
          static final int high;
          static final Integer[] cache;
          static Integer[] archivedCache;
          static {
          // high value may be configured by property
          int h = 127;
          String integerCacheHighPropValue =
          VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
          if (integerCacheHighPropValue != null) {
          try {
          h = Math.max(parseInt(integerCacheHighPropValue), 127);
          // Maximum array size is Integer.MAX_VALUE
          h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
          } catch( NumberFormatException nfe) {
          // If the property cannot be parsed into an int, ignore it.
          }
          }
          high = h;
          // Load IntegerCache.archivedCache from archive, if possible
          VM.initializeFromArchive(IntegerCache.class);
          int size = (high - low) + 1;
          // Use the archived cache if it exists and is large enough
          if (archivedCache == null || size > archivedCache.length) {
          Integer[] c = new Integer[size];
          int j = low;
          for(int i = 0; i < c.length; i++) {
          c[i] = new Integer(j++);
          }
          archivedCache = c;
          }
          cache = archivedCache;
          // range [-128, 127] must be interned (JLS7 5.1.7)
          assert IntegerCache.high >= 127;
          }
          private IntegerCache() {}
          }

          可知:

          • Integer类第一次被使用到,Integer的静态内部类就被加载,并创建-128到127的Integer对象

          • 同时创建一个数组cache来缓存这些对象。

          • 当使用valueOf()方法创建对象时,就直接返回已经缓存的对象,也就是说不会再新建对象;

          • 当使用new关键字or使用valueOf()方法创建小于-128大于127的值对象时,就会创建新对象。

          因此,当自动装箱创建的对象在[-128,127]之间时,相同数字其实指向的是同一个对象

          注意:

          • 在8种包装类型中,有缓存区的有Character、Byte、Short、Integer、Long,都是-128到127的缓存范围。

          • Boolean虽然没有缓存区,Boolean在成员变量中就创建了两个相应的对象,true,false。

          • Float、Double,没有缓存区。使用缓存区缓存它们不具备可能性和实用性。

          四、包装类的计算

          共三个步骤:

          1. 将两个包装对象分别进行拆箱

          2. 将拆箱得到的两个数值计算求其值

          3. 将值进行装箱

          注意:

          1. 包装类可以直接用运算符号进行计算

          2. 包装类不能用基本数据类型的方法进行强制类型转换

          3. 集合中无法插入基本数据类型,必须转换为包装类

          4. 四则运算,逻辑运算,位运算都为此步骤

            //错误
            Integer n = (Integer)num1;
            //正确
            Integer n = num1.intValue();






            文章转载自Hello 帅帅,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

            评论