Decode String to Integer in Java



In this article, we will learn to decode string to integer in Java. Converting a string to an integer is a routine task in Java programming. It is helpful when working with user inputs, file operations, and data fetching from databases.

Problem Statement

The goal is to convert a given string representation of a number into an integer.

For Example
Input

String val = "2";

Output

Integer = 2

Different Approaches

The following are the two different approaches to decode string to integer in Java ?

Using Integer.decode()

The Integer.decode() function converts a string to an integer. It returns an Integer object with the int value specified by the specified string value. The method has the advantage that it automatically determines various numeric notations such as Decimal (for example, "10"), Octal (for example, "010"), and Hexadecimal (for example, "0x10" or "0X10").

Following are the steps to decode string to integer using Integer.decode() ?

  • The val variable holds a string representation of a number.
  • The Integer.decode(val) method converts this string into an integer.
  • The result is printed using System.out.println().
Integer myInt = Integer.decode(val);

Example

Below is an example of decoding a string to an integer using Integer.decode() ?

public class Demo {
   public static void main(String []args) {
      String val = "2";
      Integer myInt = Integer.decode(val);
      System.out.println("Integer = " + myInt);
   }
}

Output

Integer = 2

Time Complexity: O(n), where n is the length of the string due to parsing operations.
Space Complexity: O(1), as it uses a constant amount of extra space.

Using Integer.parseInt()

A more commonly used method for converting a string to an integer is Integer.parseInt(). This method strictly processes decimal values.

Following are the steps to decode string to integer using Integer.parseInt() ?

  • The val variable holds a string representation of a number.
  • The Integer.parseInt(val) method converts this string into an integer.
  • The result is printed using System.out.println().
int myInt = Integer.parseInt(val);

Example

Below is an example of decoding a string to an integer using Integer.parseInt() ?

public class ParseIntExample {
   public static void main(String []args) {
      String val = "2";
      int myInt = Integer.parseInt(val);
      System.out.println("Integer = " + myInt);
   }
}

Output

Integer = 2

Time Complexity: O(n), where n is the length of the string due to parsing operations.
Space Complexity: O(1), as it uses a constant amount of extra space.

Conclusion

Both Integer.parseInt() and Integer.decode() both aim to decode a string to an integer, though they each have different functionalities. If you need to have multi-numeric support, then Integer.decode() should be your method of choice. But if you are always using decimals, Integer.parseInt() should be used for its ease and prevalence of usage.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-03-06T19:22:31+05:30

509 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements