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

We Are Going To Discuss About The constructors Integer(int), Double(double), Long(long) and so on are deprecated. So lets Start this Java Article.

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

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

    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.

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

    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.

Solution 1

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.)

Original Author Denys Séguret Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment