创建一个持有值1的新Integer对象? [英] create a new Integer object that holds the value 1?

查看:100
本文介绍了创建一个持有值1的新Integer对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要创建一个新的Integer对象,该对象在Java 1中保存该值,以下其中一项是正确的,并且以下方法的区别到底是什么,因为所有方法都将打印该值?

To create a new Integer object that holds the value in java 1 which one of the following is right and what exactly is the difference in the following methods as all print the value?

方法1:

 Integer p = new Integer(1);

方法2:

Integer p = 1;

方法3:

Integer p = new Integer("1");

使用方法三,我收到以下警告:

Using method three I got the following warning :

Note: HelloWorld.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details

推荐答案

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

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.

这篇关于创建一个持有值1的新Integer对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆