Java Integer Decode Method

JavaJavaBeginner
Practice Now

Introduction

The decode() method of the Java Integer class is used to decode a String to an Integer value. It can decode a decimal, hexadecimal, or octal string, and it returns the integer value of the Integer object which the String holds. In this lab, you will learn how to use the decode() method with the help of some examples.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117702{{"`Java Integer Decode Method`"}} java/exceptions -.-> lab-117702{{"`Java Integer Decode Method`"}} java/modifiers -.-> lab-117702{{"`Java Integer Decode Method`"}} java/wrapper_classes -.-> lab-117702{{"`Java Integer Decode Method`"}} java/identifier -.-> lab-117702{{"`Java Integer Decode Method`"}} java/data_types -.-> lab-117702{{"`Java Integer Decode Method`"}} java/operators -.-> lab-117702{{"`Java Integer Decode Method`"}} java/output -.-> lab-117702{{"`Java Integer Decode Method`"}} java/strings -.-> lab-117702{{"`Java Integer Decode Method`"}} java/variables -.-> lab-117702{{"`Java Integer Decode Method`"}} java/string_methods -.-> lab-117702{{"`Java Integer Decode Method`"}} java/system_methods -.-> lab-117702{{"`Java Integer Decode Method`"}} end

Understand the syntax of the decode() method

The syntax of the decode() method is:

public static Integer decode(String s) throws NumberFormatException

where:

  • s is the String to be decoded
  • NumberFormatException is thrown if the String does not contain a parsable integer.

Decode a decimal string

Add the following code in the main method:

String s = "100";
Integer decoded = Integer.decode(s);
System.out.println("Decimal decoded value is:" + decoded);

Save and run the code using the command:

javac Main.java && java Main

You will get the following output:

Enter the string to be decoded:
100
Decoded value is:100

This will decode the String "100" as a decimal and print the decoded value as 100.

Decode an octal string

Add the following code in the main method:

String s = "017";
Integer decoded = Integer.decode(s);
System.out.println("Octal decoded value is:" + decoded);

Save and run the code using the command:

javac Main.java && java Main

You will get the following output:

Enter the string to be decoded:
017
Decoded value is:15

This will decode the String "017" as an octal number and print the decoded value as 15.

Decode a hexadecimal string

Add the following code in the main method:

String s = "0x18";
Integer decoded = Integer.decode(s);
System.out.println("Hexadecimal decoded value is:" + decoded);

Save and run the code using the command:

javac Main.java && java Main

You will get the following output:

Enter the string to be decoded:
0x18
Decoded value is:24

This will decode the String "0x18" as a hexadecimal number and print the decoded value as 24.

Decode a signed hexadecimal string

Add the following code in the main method:

String s = "-0x30";
Integer decoded = Integer.decode(s);
System.out.println("Signed hexadecimal decoded value is:" + decoded);

Save and run the code using the command:

javac Main.java && java Main

You will get the following output:

Enter the string to be decoded:
-0x30
Decoded value is:-48

This will decode the String "-0x30" as a signed hexadecimal number and print the decoded value as -48.

Handle NumberFormatException

Add the following code in the main method:

String s = "abc";
try {
    Integer decoded = Integer.decode(s);
    System.out.println("Decoded value is:" + decoded);
} catch (NumberFormatException e) {
    System.out.println("Invalid String: " + s);
}

Save and run the code using the command:

javac Main.java && java Main

You will get the following output:

Enter the string to be decoded:
abc
Invalid String: abc

This will handle the NumberFormatException that can occur when decoding an invalid string.

Summary

In this lab, you learned about the syntax, parameters, return value, and usage of the decode() method of the Integer class in Java. You also got hands-on experience with the decode() method by decoding decimal, octal, and hexadecimal strings. You also learned how to handle the NumberFormatException. You can now use the decode() method to decode valid strings and get Integer values from them.

Other Java Tutorials you may like