• Java小树的参天成长【Integer缓冲区】


    # Hello!本文为个人知识总结所得,如有不足还请多多见谅;#

    在介绍Integer缓冲区之前,先来看一个面试题,问的是打印的结果是什么 

    1. public class Demo2{
    2. public static void main(string[] args) {
    3. //面试题
    4. Integer integer1=new Integer(100);
    5. Integer integer2=new Integer(100);
    6. System.out.println(integer1==integer2);//?
    7. Integer integer3=100;//白动装箱
    8. Integer integer4=100;
    9. System.out.println(integer3==integer4);//?
    10. Integer integer5=200;//自动装箱
    11. Integer integer6=200;
    12. System.out.println(integer5==integer6);//?
    13. }
    14. }

    答案:

    false
    true
    false 

    你猜对了吗?接下来我会给你讲解为什么会这样。

    首先第一个用的是构造方法,我想答案你应该知道是什么原因,这里解释第二和第三。

    Integer integer3=100;//自动装箱
    Integer integer4=100;

    Integer integer5=200;//自动装箱
    Integer integer6=200;

    其实我们看到的自动装箱详细来说应该是(见代码),也就是说自动装箱其实调用的就是Integer.valueof()。而不是构造方法

     Integer integer3=Integer.valueOf(100);//自动装箱
    Integer integer4=Integer.valueOf(100);

    Integer integer5=Integer.valueOf(200);//自动装箱
    Integer integer6=Integer.valueOf(200);

     而我们答案的原因就出在valueOf()上面,我们打开Integer源码

    1. public static Integer valueOf(int i) {
    2. if (i >= IntegerCache.low && i <= IntegerCache.high)
    3. return IntegerCache.cache[i + (-IntegerCache.low)];
    4. return new Integer(i);
    5. }

    其中的的IntegerCache就是Integer的缓冲区;

    其中IntegerCache.low和 IntegerCache.high的数值为-128和127(下面是对应的源码,可以找到对应的值)

    1. private static class IntegerCache {
    2. static final int low = -128;
    3. static final int high;
    4. static final Integer[] cache;
    5. static Integer[] archivedCache;
    6. static {
    7. // high value may be configured by property
    8. int h = 127;
    9. String integerCacheHighPropValue =
    10. VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
    11. if (integerCacheHighPropValue != null) {
    12. try {
    13. h = Math.max(parseInt(integerCacheHighPropValue), 127);
    14. // Maximum array size is Integer.MAX_VALUE
    15. h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
    16. } catch( NumberFormatException nfe) {
    17. // If the property cannot be parsed into an int, ignore it.
    18. }
    19. }
    20. high = h;

    我们也可以从代码中发现一个数组对应代码

     static final Integer[] cache;
    1. // Load IntegerCache.archivedCache from archive, if possible
    2. CDS.initializeFromArchive(IntegerCache.class);
    3. int size = (high - low) + 1;
    4. // Use the archived cache if it exists and is large enough
    5. if (archivedCache == null || size > archivedCache.length) {
    6. Integer[] c = new Integer[size];
    7. int j = low;
    8. for(int i = 0; i < c.length; i++) {
    9. c[i] = new Integer(j++);
    10. }
    11. archivedCache = c;
    12. }
    13. cache = archivedCache;
    14. // range [-128, 127] must be interned (JLS7 5.1.7)
    15. assert IntegerCache.high >= 127;
    16. }

    可以发现这个数组的长度为high-low+1 = 256;这里面存了256个数据元素,里面的for循环对数组初始化


    回到这串代码,

    1. public static Integer valueOf(int i) {
    2. if (i >= IntegerCache.low && i <= IntegerCache.high)
    3. return IntegerCache.cache[i + (-IntegerCache.low)];
    4. return new Integer(i);
    5. }

    如果i>=-128(i >= IntegerCache.low)     i<127( i <= IntegerCache.high),在这个范围内,那就从Cache数组里面取

    回到题目,100在-128与127内,返回的就是缓冲区里面的对象

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

    问题中的integer3和integer4在内存里面如图

     而200没有在这之间,在堆里面开辟空间,和构造方法一样,见代码

     return new Integer(i);

    所以答案的原因你懂了吗?

  • 相关阅读:
    原子性操作
    高级性能测试系列《20. 事务控制器、在性能测试中,看聚合报告的前提条件是?》...
    那些在开源世界顶半边天的女同胞们
    数据安全:什么是数据风险评估?等保合规为什么是企业必需品
    Docker基于本地文件安装Nacos单机版
    flutter 适配屏幕宽高工具
    阿里云视频上传实战
    Kubernetes Dashboard 部署应用以及访问
    刷完HashMap源码,我们一起进大厂
    【FreeRTOS】08 互斥信号量、优先级反转问题
  • 原文地址:https://blog.csdn.net/qq_65231448/article/details/126436608