新的整数类型 vs valueOf方法

77

我正在使用Sonar来使我的代码更加清晰,它指出我在使用new Integer(1)而非Integer.valueOf(1)。因为似乎valueOf不会实例化新对象,所以更加节省内存。但是,valueOf如何能够不实例化新对象呢?它是如何工作的?这对所有整数都成立吗?


13
注意:如果你使用自动装箱,它会为你使用Integer.valueOf(int)函数。 - Peter Lawrey
4个回答

77

我知道,在拆箱的某个地方,但我找不到那个部分。谢谢。 - LB40
1
那个部分没有明确说明valueOf的内容。装箱通常是基于valueOf实现的,但这并不是必须的。此外,也允许缓存超出该范围的值。这只是装箱的最小范围。 - Matthew Flaschen
22
完整性而言,还需注意在Sun VMs上,缓存的最大值可以通过 -XX:AutoBoxCacheMax=... 进行用户配置。 - Mark Peters
5
为了完整性,@MarkPeters,该功能仅在Sun Java 6最近的更新(我认为是14或更高版本)之后才可用。 - Jesper

30

根据JavaDoc

public static Integer valueOf(int i) 返回表示指定 int 值的 Integer 实例。如果不需要新的 Integer 实例,则通常应该使用此方法,而不是构造函数 Integer(int),因为该方法通过缓存经常请求的值,可能在空间和时间性能方面产生显着的优势。

ValueOf 通常用于自动装箱,因此(当用于自动装箱时)缓存至少从 -128 到 127 的值以遵循自动装箱规范。

这是 Sun JVM 1.5 的 valueOf 实现。查看整个类以了解如何初始化缓存。

public static Integer valueOf(int i) {
    final int offset = 128;
    if (i >= -128 && i <= 127) { // must cache 
        return IntegerCache.cache[i + offset];
    }
    return new Integer(i);
}

3
他们正在推荐您使用valueOf()而不是new Integer(),因为方法valueOf()会为您完成此操作,并在以后需要再次获取相同数字时缓存该值。在这种情况下,该方法不会实例化新的整数,而是会给您缓存的整数,从而使创建新的整数变得更快速和内存友好。
如果您是一个经验不足的Java程序员,这样做可能会给自己带来很多问题,因为您会得出结论Integer.valueOf(342)==Integer.valueOf(342),因为您可能(或可能不)拥有两个整数的相同指针,并且可能会按照您在C#中学到的方式进行实践,这将不时显示出错误,并且您将不知道这些错误来自何处...

2

从java.lang.Integer源代码中可以看出,整数缓存是可配置的。如果要配置除Sun以外的整数缓存大小,我们需要按照源代码使用系统属性java.lang.Integer.IntegerCache.high

/**
 * 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. During VM initialization the
 * getAndRemoveCacheProperties method may be used to get and remove any system
 * properites that configure the cache size. At this time, the size of the
 * cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>.
 */

// value of java.lang.Integer.IntegerCache.high property (obtained during VM init)
private static String integerCacheHighPropValue;

static void getAndRemoveCacheProperties() {
    if (!sun.misc.VM.isBooted()) {
        Properties props = System.getProperties();
        integerCacheHighPropValue =
            (String)props.remove("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null)
            System.setProperties(props);  // remove from system props
    }
}

private static class IntegerCache {
    static final int high;
    static final Integer cache[];

    static {
        final int low = -128;

        // high value may be configured by property
        int h = 127;
        if (integerCacheHighPropValue != null) {
            // Use Long.decode here to avoid invoking methods that
            // require Integer's autoboxing cache to be initialized
            int i = Long.decode(integerCacheHighPropValue).intValue();
            i = Math.max(i, 127);
            // Maximum array size is Integer.MAX_VALUE
            h = 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() {}
}

使用java.lang.Short、java.lang.Byte和java.lang.Long创建一个范围从127到-128的缓存。

private static class LongCache {
    private LongCache() {
    }

    static final Long cache[] = new Long[-(-128) + 127 + 1];

    static {
        for (int i = 0; i < cache.length; i++)
            cache[i] = new Long(i - 128);
    }
}

private static class ShortCache {
    private ShortCache() {
    }

    static final Short cache[] = new Short[-(-128) + 127 + 1];

    static {
        for (int i = 0; i < cache.length; i++)
            cache[i] = new Short((short) (i - 128));
    }
}

private static class ByteCache {
    private ByteCache() {
    }

    static final Byte cache[] = new Byte[-(-128) + 127 + 1];

    static {
        for (int i = 0; i < cache.length; i++)
            cache[i] = new Byte((byte) (i - 128));
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接