读书人

关于java中的部类比较面试题求解

发布时间: 2012-08-29 08:40:14 作者: rapoo

关于java中的类型比较面试题,求解?

1.

public static Integer valueOf(int i) {assert IntegerCache.high >= 127;if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}...private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint h = 127;String integerCacheHighPropValue =sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");if (integerCacheHighPropValue != null) {int i = parseInt(integerCacheHighPropValue);i = Math.max(i, 127);// Maximum array size is Integer.MAX_VALUEh = Math.min(i, Integer.MAX_VALUE - (-low));}high = h;cache = new Integer[(high - low) + 1];int j = low;for(int k = 0; k < cache.length; k++)cache[k] = new Integer(j++);}private IntegerCache() {}}

至于情况(1),可能为true,也可能为false,java要求-128到127必须缓存,所以在这个范围内的情况(1)一定为true,超出这个范围的看具体实现和参数,jdk7就有参数可以调整这个缓存的大小。 9 楼 ps329795485 2012-03-03 以前面试也有遇到这问题

读书人网 >编程

热点推荐