且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建一个持有值1的新Integer对象?

更新时间:2023-11-30 14:25:22

您跳过了预期的解决方案:

You skipped the intended solution:

Integer p = Integer.valueOf(1);

此模式称为工厂方法模式.有人可能会问这种方法有什么好处.幸运的是,

This pattern is known as Factory method pattern. One may ask what the benefit of this method is. Luckily, the implementation of class Integer is open-source, so let's take a look:

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

似乎有某种Integer值缓存.如果一个请求的Integer的值在缓存范围内,则Java不会创建新的对象,而是返回先前创建的对象.这是可行的,因为Integer是不可变的.甚至可以使用系统属性java.lang.Integer.IntegerCache.high=...控制高速缓存上限.

There seems to be some kind of Integer-value cache. If one requests an Integer with a value within the cache-range, Java does not create a new object, but returns a previously created one. This works because Integers are immutable. One can even control the upper cache limit with the system property java.lang.Integer.IntegerCache.high=....

为什么其他两种创建Integer的方法会生成警告?因为它们已被Java 9弃用.

And why do the other two methods of creating an Integer generate a warning? Because they were set deprecated with Java 9.

Integer#Integer(int value) :

已弃用.很少适合使用此构造函数.通常,静态工厂valueOf(int)是更好的选择,因为它可能产生明显更好的空间和时间性能. [...]

Deprecated. It is rarely appropriate to use this constructor. The static factory valueOf(int) is generally a better choice, as it is likely to yield significantly better space and time performance. [...]

Integer#Integer(String s) :

已弃用.很少适合使用此构造函数.使用parseInt(String)将字符串转换为int原语,或使用valueOf(String)将字符串转换为Integer对象. [...]

Deprecated. It is rarely appropriate to use this constructor. Use parseInt(String) to convert a string to a int primitive, or use valueOf(String) to convert a string to an Integer object. [...]

为了完整起见,这是 Integer.valueOf(int i) :

And just for completeness, here is the part for Integer.valueOf(int i):

返回表示指定的int值的Integer实例.如果不需要新的Integer实例,则通常应优先于构造方法Integer(int)使用此方法,因为此方法通过缓存经常请求的值可能会产生明显更好的空间和时间性能.此方法将始终缓存-128127(包括)范围内的值,并且可能缓存该范围之外的其他值.

Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.


感谢 @VGR提到了

Integer p = 1;

等价于

Integer p = Integer.valueOf(1);

但是,这仅适用于-128127之间的int值.该行为在 JLS中定义§5.1.7:

This, however, is only true for int-values between -128 and 127. The behaviour is defined in JLS §5.1.7:

[...]如果装箱的值p是对类型为booleancharshortintlong的常量表达式(第15.28节)求值的结果,结果为truefalse'\u0000''\u007f'范围内的字符或-128127范围内的整数,然后让ap的任何两次装箱转换的结果. a == b总是如此.

[...] If the value p being boxed is the result of evaluating a constant expression (§15.28) of type boolean, char, short, int, or long, and the result is true, false, a character in the range '\u0000' to '\u007f' inclusive, or an integer in the range -128 to 127 inclusive, then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.


感谢 @DorianGray,他引起了我的注意.

虽然不在JLS中,但我正在使用的javac版本(9.0.4)确实将拳击编译为Integer.valueOf(...);,如

While not in the JLS, the version of javac I am using (9.0.4) does compile the boxing down to Integer.valueOf(...); as it is shown in this answer by Adam Rosenfield.