In this tutorial, you will learn how to update the deprecated code In Java. To realize this, you will create an application that converts a string to an integer. In the first section, the application will use deprecated code. In the second section, you will update the deprecated code.

When developing applications using Java. You will often encounter legacy code that is no longer supported. If you encounter the annotation @Deprecated in Java, it means that the code is no longer supported.

Project setup

This blog has a tutorial that shows how to create a project in IntelliJ. Go to the tutorial and follow through to create a new project. Select IntelliJ in the Build System section to create a console application.

Deprecated code

In your base package, create a file named WithDeprecation.java. Copy and paste the following code into the file.

package com.javawhizz;

public class WithDeprecation {
    public Integer stringToInt() {
        String number = "200";

        //FIXME: This constructor is deprecated
        return new Integer(number);
    }
}

The stringToInt() method starts by declaring and initializing the number variable to “200”.

To convert the String to an Integer, create a new instance of Integer and pass the variable as the argument. As a result, IntelliJ displays a warning because the approach is not supported.

Calling the Integer class using the new keyword is a deprecated approach. If you hover your mouse over the warning, IntelliJ displays the following message.

deprecated code warning

You can also press and hold the CTRL command then click on Integer class to open the Java documentation for it.

Click download sources to download the documentation for the class. The following documentation gets added to the code if the download is successful.

Additionally, you can go to docs.oracle.com to get an online version of the Java documentation. If it’s convenient for you, you can also download a copy of the documentation.

deprecated code documentation

Run the code

IntelliJ creates a file named Main.java when creating a new project. Copy and paste the following code into the file.

import com.javawhizz.WithDeprecation;

public class Main {
    public static void main(String[] args) {
        WithDeprecation withDeprecation =
                new WithDeprecation();
        System.out.println(withDeprecation.stringToInt());
    }
}

Output

200

Update the deprecated code

To update the deprecated code to a recommended approach. You can use either the warning message or the Java documentation.

Both of these approaches provide a description of what the class does. Additionally, they provide alternative approaches to use in case of deprecation.

For example, you can use either the parseInt() or valueOf() methods to convert a String to an Integer.

In the base package, create a file named WithoutDeprecation.java. Next, copy and paste the following code into the file.

package com.javawhizz;

public class WithoutDeprecation {
    public Integer stringToInt() {
        String number = "200";

        return Integer.parseInt(number);

//        return Integer.valueOf(number);
    }
}

Run the code

Copy and paste the following code into the Main.java file.

import com.javawhizz.WithoutDeprecation;

public class Main {
    public static void main(String[] args) {
        WithoutDeprecation withoutDeprecation =
                new WithoutDeprecation();

        System.out.println(withoutDeprecation.stringToInt());
    }
}

Output

200

Conclusion

In this tutorial, you have learned how to update deprecated code in Java. You first used deprecated code then updated the deprecated code for your code.

When you encounter deprecated code, use Java documentation to get alternative approaches. Another useful tool to use that we have learned is the IDE warning messages.

With this in mind, you will always ensure that your code is not using code that might lead to bugs.

Happy Hacking!

Categories: Core Java

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *