zoukankan      html  css  js  c++  java
  • 走近Java之包装器类Integer

    前几天,有个同事问了我一个关于Integer类赋值的问题,很有意思,我们一起来看一下(如果有说的不正确的地方,欢迎大家指正)。

    image
    如上图,同样是赋值,但是两次比较的结果完全不同。我们走近了解一下。
    在Integer中,有一个静态内部类IntegerCache,其内有一个Integer缓存数组,范围从-128到127
    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是处理VM入参,可忽略
            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;
            //构造一个长度是256的Integer数组
            cache = new Integer[(high - low) + 1];
            int j = low;
            //遍历赋值-128到127
            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的valueOf方法,此方法会判断入参大小,如果在-128到127之间,就直接返回cache数组中的值,否则才会创建Integer对象。
    image
     
    再看另外一个问题.为什么i的结果都是5.
    image
    Integer是不可变类。在Integer类中,value被定义为final,每一次赋值方法返回的都是新的对象,而不是改变value的值。也是因为这个原因,为了提高性能,所以才有cache的Integer数组。
    image
     Java中的8个包装器类,以及String,均是不可变类。
    作者:wangl110
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    LeetCode Flatten Binary Tree to Linked List
    LeetCode Longest Common Prefix
    LeetCode Trapping Rain Water
    LeetCode Add Binary
    LeetCode Subsets
    LeetCode Palindrome Number
    LeetCode Count and Say
    LeetCode Valid Parentheses
    LeetCode Length of Last Word
    LeetCode Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/wangl110/p/10765696.html
Copyright © 2011-2022 走看看