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

Answer from Denys Séguret on Stack Overflow
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › String-to-long-in-Java
String to long in Java
Main.java:9: warning: [removal] Long(String) in Long has been deprecated and marked for removal long face = new Long( textString );
Discussions

Deprecation warnings when compiling on JDK > 9: Integer(int) in Integer has been deprecated and marked for removal
The use of new Integer(int) here is deprecated in JDK 9 and marked for removal, see Deprecation of Boxed Primitive Constructors. // Note: We must use "new Integer(number)" here because we don't wan... More on github.com
🌐 github.com
2
April 4, 2021
Compilation warnings in Java 17
[INFO] Compiling 42 source files with javac [debug target 1.8] to target/test-classes Warning: bootstrap class path not set in conjunction with -source 8 Warning: /home/runner/work/JSON-java/JSON-j... More on github.com
🌐 github.com
3
September 2, 2023
Integer has been deprecated and marked for removal
JRJdk13Compiler shows theses warnings:warning: [removal] Integer(int) in Integer has been deprecated and marked for removal value = new java.lang.Integer(1); //$JR_EXPR_ID=1$Most probably this deprecated syntax (new java.lang.Integer(int)) is used for initialization of Integer variables defined i... More on community.jaspersoft.com
🌐 community.jaspersoft.com
5
May 16, 2023
java - What is the proper way to mark the removal version using @Deprecated? - Stack Overflow
I was asked to deprecate a couple classes to be deprecated and removed in a future release. Given the Javadocs there are 2 properties, the version it was deprecated in and whether or not it will be More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 3
2

In the general case, you can use Long::valueOf. But in this case specifically, you should take the time to simplify the code in general. The code you have is more reminiscent of Java 1.4 (before generics) than of Java 8, even.

Here's your comparator, nicely formatted:

new Comparator() {
    public int compare(final Object o1,final Object o2) {
        return byAscendingDate
            ? new Long(((File)o1).lastModified()).compareTo(new Long(((File) o2).lastModified()))
            : new Long(((File)o2).lastModified()).compareTo(new Long(((File) o1).lastModified()));
        }
    });

First, use the generic form instead of a raw Comparator. This remove the need for casts:

new Comparator<File>() { // use generic instead of raw
    public int compare(final File o1, final File o2) { // File instead of Object
        return byAscendingDate
            ? new Long(o1.lastModified()).compareTo(new Long(o2.lastModified()))
            : new Long(o2.lastModified()).compareTo(new Long(o1.lastModified()));
        }
    });

Now, instead of new Long(...).compareTo or even Long.valueOf(...).compareTo, you can just use the static Long::compare(long, long) introduced in Java 7:

new Comparator<File>() { // use generic instead of raw
    public int compare(final File o1, final File o2) { // File instead of Object
        return byAscendingDate
            ? Long.compare(o1.lastModified(), o2.lastModified())
            : Long.compare(o2.lastModified(), o1.lastModified())
        }
    });

Then, you can use lambdas instead of an anonymous class:

(o1, o2) -> byAscendingDate
     ? Long.compare(o1.lastModified(), o2.lastModified())
     : Long.compare(o2.lastModified(), o1.lastModified())

Or, you can even just use the Comparator helpers introduced in Java 8. Combining Comparator::comparingLong and Comparator::reversed, you get:

Comparator<File> cmp = Comparator.comparingLong(File::lastModified);
if (!byAscendingDate) {
    cmp = cmp.reversed();
}
Arrays.sort(files, cmp);

I would encourage you to look around the Comparator and Long classes to see what new functions are around, and in general to keep an eye out for the various helpers that have been introduced over the years.

2 of 3
1

You can use the Long#valueOf method, as per the JavaDoc specification.

Long – Constructor Details (Java SE 20 & JDK 20).

"Deprecated, for removal: This API element is subject to removal in a future version.
It is rarely appropriate to use this constructor. The static factory valueOf(long) is generally a better choice, as it is likely to yield significantly better space and time performance."
.

Although, since you are comparing two long primitive values, you can just use the Long#compare method.
This will remove the need to create a Long object.

Additionally, you can replace the anonymous-inner class, Comparator, with a closure.

Arrays.sort(files, (Comparator<File>) (o1, o2) -> byAscendingDate
    ? Long.compare(o1.lastModified(), o2.lastModified())
    : Long.compare(o2.lastModified(), o1.lastModified()));
🌐
Softwaregarden
softwaregarden.dev › en › posts › new-java › ready-for-valhalla
Are you ready for Valhalla? – SoftwareGarden.dev
May 27, 2021 - Luckily for us, Intellij IDEA marks this okay (thankfully this warning doesn’t require indexing, it seems ;-) ). It might be you don’t compile the sources yourself, but instead you’re using some JARs provided by or fetched from 3rd party / external vendor. Fear not, then just use jdeprscan and Bob’s your uncle! $ jdeprscan goodies/target/goodies-1.0-SNAPSHOT.jar Jar file goodies/target/goodies-1.0-SNAPSHOT.jar: class ValueBasedClasses uses deprecated method java/lang/Long::<init>(J)V (forRemoval=true)
🌐
Eclipse
bugs.eclipse.org › bugs › show_bug.cgi
580917 – Fix deprecated warnings - Eclipse bug
Download · Getting Started · Members · Projects · Community · Marketplace · Events · Planet Eclipse · Videos · Participate
🌐
GitHub
github.com › protocolbuffers › protobuf › issues › 8449
Deprecation warnings when compiling on JDK > 9: Integer(int) in Integer has been deprecated and marked for removal · Issue #8449 · protocolbuffers/protobuf
April 4, 2021 - The use of new Integer(int) here is deprecated in JDK 9 and marked for removal, see Deprecation of Boxed Primitive Constructors. // Note: We must use "new Integer(number)" here because we don't wan...
Author   protocolbuffers
🌐
OneCompiler
onecompiler.com › java › 3vk58qh4y
3vk58qh4y - Java - OneCompiler
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast.
🌐
OneCompiler
onecompiler.com › java › 3ybas9a6c
3ybas9a6c - Java - OneCompiler
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 773726 › java › replace-statement-deprecated-constructor
Should I replace a statement with a deprecated constructor? (Java in General forum at Coderanch)
June 28, 2023 - If this constructor has been deprecated, what's its substitute? The Java API has apparently not been updated. ... The javadocs you link to are about Java 7, so they can't talk about this. More current documentation (like https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html) will tell you what to use instead. And no, you should not suppress warnings that you don't understand.
🌐
Apache JIRA
issues.apache.org › jira › browse › JDO-810
[#JDO-810] Fix Java 9 warnings - ASF JIRA
Public signup for this instance is disabled. Go to our Self serve sign up page to request an account. Report potential security issues privately · Fix warnings from Java 9, especially cponstructor calls on primitive Wrappers such as "Integer(5)" or "Long(2)" (note that AccessControler and ...
🌐
GitHub
github.com › stleary › JSON-java › issues › 766
Compilation warnings in Java 17 · Issue #766 · stleary/JSON-java
September 2, 2023 - ... [INFO] Compiling 42 source files with javac [debug target 1.8] to target/test-classes Warning: bootstrap class path not set in conjunction with -source 8 Warning: /home/runner/work/JSON-java/JSON-java/src/test/java/org/json/junit/JSONObjectTest.java:[307,28] Boolean(boolean) in java.lang.Boolean has been deprecated and marked for removal Warning: /home/runner/work/JSON-java/JSON-java/src/test/java/org/json/junit/JSONObjectTest.java:[308,29] Boolean(boolean) in java.lang.Boolean has been deprecated and marked for removal Warning: /home/runner/work/JSON-java/JSON-java/src/test/java/org/json/
Author   stleary
🌐
Oracle
docs.oracle.com › en › java › javase › 23 › core › notifications-and-warnings.html
Notifications and Warnings - Java
January 17, 2025 - public class RemovalExample { public static void main(String[] args) { System.runFinalizersOnExit(true); } } $ javac RemovalExample.java RemovalExample.java:3: warning: [removal] runFinalizersOnExit(boolean) in System has been deprecated and marked for removal System.runFinalizersOnExit(true); ^ 1 warning ==========
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8254324
[JEP 390] Deprecate wrapper class constructors for removal
@Deprecated(since="9", forRemoval = true) public java.lang.Boolean(boolean) {...} @Deprecated(since="9", forRemoval = true) public java.lang.Boolean(java.lang.String) {...} @Deprecated(since="9", forRemoval = true) public java.lang.Byte(byte) {...} @Deprecated(since="9", forRemoval = true) public java.lang.Byte(java.lang.String) throws java.lang.NumberFormatException {...} @Deprecated(since="9", forRemoval = true) public java.lang.Character(char) {...} @Deprecated(since="9", forRemoval = true) public java.lang.Short(short) {...} @Deprecated(since="9", forRemoval = true) public java.lang.Short(
🌐
Jaspersoft Community
community.jaspersoft.com › home › forum › integer has been deprecated and marked for removal
Integer has been deprecated and marked for removal - Products - Jaspersoft Community
May 16, 2023 - JRJdk13Compiler shows theses warnings:warning: [removal] Integer(int) in Integer has been deprecated and marked for removal value = new java.lang.Integer(1); //$JR_EXPR_ID=1$Most probably this deprecated syntax (new java.lang.Integer(int)) is used for initialization of Integer variables defined i...
🌐
Eclipse
bugs.eclipse.org › bugs › show_bug.cgi
489986 – Replace new Integer() with Integer.valueOf() in org.eclipse.test
Download · Getting Started · Members · Projects · Community · Marketplace · Events · Planet Eclipse · Videos · Participate
Top answer
1 of 2
5

This problem is pretty well-known to be honest, and I can share some experience here.

First, there is no in-built way in java to specify the version of the software, in which the given API would be deleted. The parameter since is merely the version in which the API was marked for deprecation, quote from the doc:

The value of this element indicates the version in which the annotated program element was first deprecated.

I'm a community contributor to spring-data open source projects, and the API deprecation is a common thing. In case we deprecate some API, if we mark that forRemoval = true, it is simply removed in the next major release following the SemVer. I'm just letting you know how it is done by the hegemonies of the industry. As for this statement:

Also there is a since property, however, since we have multiple versions in parallel this doesn't seem intuitive. For example we might release a patch for version 4.3, 4.2 and 4.1 so technically on the 4.3 branch it would be marked deprecated since say 4.3.2 and on the 4.2 it would be since 4.2.5 and the 4.1 would be since 4.1.9. This means if you are on 4.3.1, 4.2.4, or 4.1.8 it won't be marked deprecated.

This is totally fine. This is one of the many reasons why the previous major versions are still supported - because some API that is deleted in the major version 2 is present in the major version 1, and some client is not ready yet to migrate. That is why we're backporting patches into previous major release minor version, as you described, thats fine. This practice is adopted by JDK, Kotlin, Spring projects etc.

Am I relegated to comments to make this work or is there some way I can do all this with the annotations so it is recognized by the developer's IDE?

There is no compatible way to do this. You cannot know what IDE the person is using and what plugins or inspections are enabled.

What we can and should do is:

  1. Add Java tag @deprecated and provide the meaningful explanation why the API is deprecated and when it will be removed.
  2. Add Java tag @see to guide the user to the potential replacement for the API
  3. And of course, do not forget to package the sources along with the .jar that contains the bytecode in order for the two suggestions above to work on developer's IDE, at least IntelliJ can download and parse the source jars.

Caring about users is a great thing, you're doing the right thing. But you cannot solve everything, so, do the best with what you've got.

P.S: Oracle official guide on deprecation of the API, from their perspective.

2 of 2
0

I would say in may case the solution was to not mark it deprecated in the older versions as people would have to upgrade to get to the deprecation warning. Given that we are using OpenRewrite this seemed like a valid narrative so instead we moved the @Deprecated to just the latest branch and marked the since at that branch.

Not going to accept an answer though as this seems like a subjective thing.

🌐
O'Reilly
oreilly.com › library › view › java-cookbook › 0596001703 › ch01s10.html
Dealing with Deprecation Warnings - Java Cookbook [Book]
June 21, 2001 - Dealing with Deprecation WarningsProblemYour code used to compile cleanly, but now gives deprecation warnings. SolutionYou must have blinked :-). Either live with the warnings --... - Selection from Java Cookbook [Book]
Author   Ian F. Darwin
Published   2001
Pages   888
🌐
Reddit
reddit.com › r/java › jdk 9 deprecates number constructors, e.g. integer(int)
r/java on Reddit: JDK 9 deprecates Number constructors, e.g. Integer(int)
September 16, 2016 - Will the constructors be removed in Java 10+? If not, then it won't change anything as long as there are old libraries or deprecation-ignoring authors around. ... Not really, but they plan to eventually change that. ... It's just displays a warning from compilers and IDEs, but it's better than nothing. The list of deprecated stuff is long and includes methods that have been deprecated for 20 years and are still there and working.