妖妖っ凌
重回博客园,每天积累一点点。。

导航

 

  最近想换工作,bat等一线互联网公司都面了个遍,被轮流虐了无数遍之后发现欠缺很多,总结一下:

  1.工作中只知道尽快的完成任务,实现功能,很多时候想到一种解决方案就下手去干了,是不是最优方案、数据量大了之后的性能问题、并发安全问题等都欠考虑。

  2.本身不是计算机科班出身,基础薄弱,线程、锁、网络等计算机基础掌握不牢,源码看的偏少,很多常用的类都没仔细看过。

  3.数据结构和算法功底差,只能写个最简单的冒泡排序。。最多再能写个汉诺塔之类的递归。

 

  问题多多,好在一次腾讯面试的时候,面试官大姐指点说可以多看点源码,可以了解底层是怎么实现的,然后考虑为什么这么实现,举一反三,再想想如果自己实现的话应该怎么做,受到启发之后觉得这确实是一个好办法,于是决定从今天起每天读点源码(周六日要打球,身体是革命的本钱),从JDK源码开始吧。

 

  Integer

  进制转换方法,toString(int i,int radix)

  首先定义好所有可能出现的字符digits,数字+字母,长度为36

  final static char[] digits = {
        '0' , '1' , '2' , '3' , '4' , '5' ,
        '6' , '7' , '8' , '9' , 'a' , 'b' ,
        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
        'o' , 'p' , 'q' , 'r' , 's' , 't' ,
        'u' , 'v' , 'w' , 'x' , 'y' , 'z'
    };

  方法正文:

 

public static String toString(int i, int radix) {
    
    //Min/Max_RADIX分别为2,36
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) radix = 10; /* Use the faster version */ if (radix == 10) { return toString(i); } char buf[] = new char[33]; boolean negative = (i < 0); int charPos = 32; if (!negative) { i = -i; } while (i <= -radix) { buf[charPos--] = digits[-(i % radix)]; i = i / radix; } buf[charPos] = digits[-i]; if (negative) { buf[--charPos] = '-'; } return new String(buf, charPos, (33 - charPos)); }

 

  源码慢慢看,先来看能解决一些疑惑的部分源码

  

    Integer c = 10;
        Integer d = 10;
        Integer e = 130;
        Integer f = 130;
        System.out.println(c==d);
        System.out.println(e==f);

  上述代码输出为:true,false,为什么呢?看一下源码:

  

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

  进一步查看IntegerCache:

  

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在采用Integer a =xx方式赋值的时候,首先会从内部的缓存中取,缓存的范围是-128-127,在初始化的时候IntegerCache 为数组cache赋值new Integer(-128-127),如果xx在这个范围中,则直接从缓存中取,否则采用new Integer(xx)赋值,这就解释了测试代码为什么输出TRUE和FALSE了,10在缓存范围中,因此c和d是同一个Integer,而130超出了缓存范围,是两个不同的new Integer,故返回FALSE。

 

  

posted on 2015-09-21 08:49  妖妖っ凌  阅读(358)  评论(0)    收藏  举报