Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The constructors Integer(int), Double(double), Long(long) and so on are deprecated

Tags:

java

While working, I got the warning

The constructor Integer(int) is deprecated 

and I couldn't find an alternative constructor/solution online. How can I resolve this issue ?

UPDATE

I will get a similar warning with constructors for other primitive wrapper types; e.g.

The constructor Boolean(boolean) is deprecated The constructor Byte(byte) is deprecated The constructor Short(short) is deprecated The constructor Character(char) is deprecated The constructor Long(long) is deprecated The constructor Float(float) is deprecated The constructor Double(double) is deprecated 

Does the same solution apply to these classes as for Integer?

like image 317
DeSpeaker Avatar asked Nov 03 '17 12:11

DeSpeaker


People also ask

Is integer class deprecated?

Deprecated. It is rarely appropriate to use this constructor.

How do you use integer classes?

Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with an int value like converting it to a string representation, and vice-versa. An object of the Integer class can hold a single int value.


1 Answers

You can use

Integer integer = Integer.valueOf(i); 

From the javadoc of the constructor:

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. Constructs a newly allocated Integer object that represents the specified int value.

The main difference is that you won't always get a new instance with valueOf as small Integer instances are cached.


All of the primitive wrapper types (Boolean, Byte, Char, Short, Integer, Long, Float and Double) have adopted the same pattern. In general, replace:

    new <WrapperType>(<primitiveType>) 

with

    <WrapperType>.valueOf(<primitiveType>) 

(Note that the caching behavior mentioned above differs with the type and the Java platform, but the Java 9+ deprecation applies notwithstanding these differences.)

like image 68
Denys Séguret Avatar answered Oct 02 '22 05:10

Denys Séguret