有趣的面试题

前段时间在微博里看到一个有趣的面试题

有趣的面试题_第1张图片
迷之面试题

当时看到这面试题的时候第一反应是WTF这怎么可能,题出错了吧,后来转念一想可能是我太无知了呢。于是我在网上搜索了半天总算有了一点眉目,我理解的要解这道题的中心点有两个,一个是Integer缓存另外一个就是自动装箱和拆箱
搞JAVA的都不会陌生除了int能这个基础类型能表示整数以外还有一个Integer类,但是有多少人去研究了Integer的源代码呢?在Integer的源码里定义了一个内部类IntegerCache,这个类会把-128~127之间的整数缓存起来,当发生自动装箱的时候会优先使用这些缓存的整数。

 /**
  * Cache to support the object identity semantics of autoboxing for values between
  * -128 and 127 (inclusive) as required by JLS.
  *
  * The cache is initialized on first usage.  The size of the cache
  * may be controlled by the {@code -XX:AutoBoxCacheMax=} option.
  * During VM initialization, java.lang.Integer.IntegerCache.high property
  * may be set and saved in the private system properties in the
  * sun.misc.VM class.
  */
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的缓存我们可以写一个method来“偷天换日”,改写IntegerCache里面的内容需要用到反射

private static void method(int a, int b) {
        try {
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            Class cls = Class.forName("java.lang.Integer$IntegerCache");
            Field f = cls.getDeclaredField("cache");
            f.setAccessible(true);
            modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
            Integer[] array = (Integer[]) f.get(cls);
            array[128 + 10] = new Integer(100);
            array[128 + 20] = new Integer(200);
            array[128 + 21] = new Integer(22);
        } catch (Exception ex) {
        }
    }

写到这里各位读者可能觉得“啊,终于搞定了,今天将会是元气满满的一天呢#笑脸”那就错了,因为上面我们只降到了Integer的缓存还没有用到自动装箱部分呢。
在题目中可以看到他们是用syso输出的[用eclipse的同学应该知道直接输入syso然后按alt+/是能自动填充为System.out.println()]但是这样的输出方式并不会调用自动装箱,需要把System.out.println()这样的输出形式改成用占位符去输出比如用System.out.printf("a=%d ", a)就可以了。
最后,祝大家元气满满!

你可能感兴趣的:(有趣的面试题)