创建一个持有值1的新Integer对象? [英] create a new Integer object that holds the value 1?
问题描述
要创建一个新的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 Integer
s 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.
已弃用.很少适合使用此构造函数.通常,静态工厂
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. [...]
已弃用.很少适合使用此构造函数.使用
parseInt(String)
将字符串转换为int
原语,或使用valueOf(String)
将字符串转换为Integer
对象. [...]
Deprecated. It is rarely appropriate to use this constructor. Use
parseInt(String)
to convert a string to aint
primitive, or usevalueOf(String)
to convert a string to anInteger
object. [...]
为了完整起见,这是 Integer.valueOf(int i)
:
And just for completeness, here is the part for Integer.valueOf(int i)
:
返回表示指定的
int
值的Integer
实例.如果不需要新的Integer
实例,则通常应优先于构造方法Integer(int)
使用此方法,因为此方法通过缓存经常请求的值可能会产生明显更好的空间和时间性能.此方法将始终缓存-128
到127
(包括)范围内的值,并且可能缓存该范围之外的其他值.
Returns an
Integer
instance representing the specifiedint
value. If a newInteger
instance is not required, this method should generally be used in preference to the constructorInteger(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
to127
, inclusive, and may cache other values outside of this range.
感谢 @VGR提到了
Integer p = 1;
等价于
Integer p = Integer.valueOf(1);
但是,这仅适用于-128
和127
之间的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
是对类型为boolean
,char
,short
,int
或long
的常量表达式(第15.28节)求值的结果,结果为true
,false
,'\u0000'
至'\u007f'
范围内的字符或-128
至127
范围内的整数,然后让a
和是 p
的任何两次装箱转换的结果.a == b
总是如此.
[...] If the value
p
being boxed is the result of evaluating a constant expression (§15.28) of typeboolean
,char
,short
,int
, orlong
, and the result istrue
,false
, a character in the range'\u0000'
to'\u007f'
inclusive, or an integer in the range-128
to127
inclusive, then leta
andb
be the results of any two boxing conversions ofp
. It is always the case thata == b
.
虽然不在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屋!