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.

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