zoukankan      html  css  js  c++  java
  • Integer诡异特性

    package 代码测试;
    
    public class ceshi {
        public static void main(String[] args) {
            Integer i1=100;
            Integer j1=100;
            System.out.println(i1==j1);
            
            Integer i2=129;
            Integer j2=129;
            System.out.println(i2==j2);
        }
    
    }

    输出结果

    输出结果不一样主要是因为与IntegerCache类有关 为了避免重复创建对象,对INteger值做了缓存,如果这个值在缓存的范围内直接返回缓存对象,否则new一个新的对象返回。

    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() {}                                                                                       }
    

    这是一个内部静态类,该类只能在Integer这个类的内部访问,这个类在初始化的时候,会去加载JVM的配置,如果有值,就用配置的值初始化缓存数组,否则就缓存-128到127之间的值。

    借鉴于https://blog.csdn.net/qq_37180608/article/details/73953308

  • 相关阅读:
    流媒体服务器搭建
    netcore问题总结
    Linux(Debian)网卡设置
    windows服务器让WEB通过防火墙的问题
    经典算法一 --- 过桥问题
    MySQL字段属性介绍
    什么是流程控制
    终于决定了,转行。
    浅析Openflow
    JavaScript 中 this 的详解
  • 原文地址:https://www.cnblogs.com/1061321925wu/p/9821137.html
Copyright © 2011-2022 走看看