128陷阱

学习128陷阱之前我们先明确几个概念:

         对象包装器:

         有时需要将 int 这样的基本类型转换为对象。 所有的基本类型都冇一个与之对应的类。 例如,Integer 类对应基本类型 int。通常, 这些类称为包装器 ( wrapper ) 这些对象包装器类拥有很明显的名字:Integer、Long、Float、Double、Short、Byte、Character 、Void 和 Boolean (前 6 个类派生于公共的超类 Number)。对象包装器类是不可变的,即一旦构造了包装器,就不允许更改包装在其中的值。

          装箱:把基本类型数据转成对应的包装类对象。

                   方式一:Integer i = Integer.value(13);

                   方式二:Integer i = new Integer(13);

          拆箱:把包装类对象转成对应的基本数据类型数据。

                                  int value = i.intValue();

         自动装箱:    int 类型的元素到 ArrayLisKlntegeP 中。调用 list.add(3); 将自动地变换成 list.add (Integer.value0f(3)); 这种变换被称为自动装箱.

         自动拆箱:   当将一个 Integer 对象赋给一个 int 值时, 将会自动地拆箱。也就是说,编译器 将下列语句: int n = list.get(i); 翻译成 int n = list.get(i).intValue()

这就是java包装类,但是也因此的情况,在使用装箱和拆箱时候产生了128陷阱,先来看一个128陷阱的例子

public class Test {
	public static void main(String[] args) {
		Integer num1 = 100;
		Integer num2 = 100;
		System.out.println(num1 == num2);
		
		Integer num3 = 128;
		Integer num4 = 128;
		System.out.println(num3 == num4);
	}
}

第一个System.out.println(num1 == num2);输出的是true,
第二个System.out.println(num3 == num4);输出的是flase。

当包装类数值超过127时候就会出现128陷阱,而128陷阱的答案就在Integet的valueOf()方当中,

       首先看一下==比较的是什么,它比较的是栈中数据是否相同,我们都知道,基本类型的数据的变量名和值都存在栈中(作为类的属性的情况除外),引用类型会把它的值存在堆中,栈中存储的是变量名及堆中数据的地址。

接着看一下源码

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) {
                try {
                    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);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;
 
            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
 
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }
 
        private IntegerCache() {}
    }

        这里说明,如果我们的数值在-128-127之间的数值都存储在有一个catch数组当中,该数组相当于一个缓存,当我们在-128-127之间进行自动装箱的时候,我们就直接返回该值在内存当中的地址,所以在-128-127之间的数值用==进行比较是相等的。而不在这个区间的数就会在堆中开辟新的地址,所以不相等。

       最后在再说一句当我们用包装类比较的时候用equals方法来比较,因为这个方法被被包装类重写过比较的是两个变量存在堆中的值是否相等

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值