Java Convert String to Integer

Introduction

Converting a String to an int, or its respective wrapper class Integer, is a common and simple operation. The same goes for the other way around, converting a Integer to String.

There are multiple ways to achieve this simple conversion using methods built-in to the JDK.

Converting String to Integer

For converting String to Integer or int, there are four built-in approaches. The wrapper class Integer provides a few methods specifically for this use - parseInt(), valueOf() and decode(), although we can also use its constructor and pass a String into it.

These three methods have different return types:

  • parseInt()- returns primitive int.
  • valueOf() - returns a new or cached instance of Integer
  • decode() - returns a new or cached instance of Integer

That being said, it's valid to raise a question:

"What's the difference between valueOf() and decode() then?

The difference is that decode() also accepts other number representations, other than regular decimal - hex digits, octal digits, etc.

Note: It's better practice to instantiate an Integer with the help of the valueOf() method rather than relying on the constructor. This is because the valueOf() method will return a cached copy for any value between -128 and 127 inclusive, and by doing so will reduce the memory footprint.

Integer.parseInt()

The parseInt() method ships in two flavors:

  • parseInt(String s) - Accepting the String we'd like to parse
  • parseInt(String s, int radix) - Accepting the String as well as the base of the numeration system

The parseInt() method converts the input String into a primitive int and throws a NumberFormatException if the String cannot be converted:

String string1 = "100";
String string2 = "100";
String string3 = "Google";
String string4 = "20";

int number1 = Integer.parseInt(string1);
int number2 = Integer.parseInt(string2, 16); 
int number3 = Integer.parseInt(string3, 32); 
int number4 = Integer.parseInt(string4, 8); 

System.out.println("Parsing String \"" + string1 + "\": " + number1);
System.out.println("Parsing String \"" + string2 + "\" in base 16: " + number2);
System.out.println("Parsing String \"" + string3 + "\" in base 32: " + number3);
System.out.println("Parsing String \"" + string4 + "\" in base 8: " + number4);

Running this piece of code will yield:

Parsing String "100": 100
Parsing String "100" in base 16: 256
Parsing String "Google" in base 32: 562840238
Parsing String "20" in base 8: 16

Integer.valueOf()

The valueOf() ships in three flavors:

  • valueOf(String s) - Accepts a String and parses it into an Integer
  • valueOf(int i) - Accepts an int and parses it into an Integer
  • valueOf(String s, int radix) - Accepts a String and returns an Integer representing the value and then parses it with the given base

The valueOf() method, unlike the parseInt() method, returns an Integer instead of a primitive int and also throws a NumberFormatException if the String cannot be converted properly and only accepts decimal numbers:

int i = 10;
String string1 = "100";
String string2 = "100";
String string3 = "Google";
String string4 = "20";

int number1 = Integer.valueOf(i);
int number2 = Integer.valueOf(string1); 
int number3 = Integer.valueOf(string3, 32); 
int number4 = Integer.valueOf(string4, 8); 

System.out.println("Parsing int " + i + ": " + number1);
System.out.println("Parsing String \"" + string1 + "\": " + number2);
System.out.println("Parsing String \"" + string3 + "\" in base 32: " + number3);
System.out.println("Parsing String \"" + string4 + "\" in base 8: " + number4);

Running this piece of code will yield:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Parsing int 10: 10
Parsing String "100": 100
Parsing String "Google" in base 32: 562840238
Parsing String "20" in base 8: 16

Integer.decode()

The decode() method accepts a single parameter and comes in one flavor:

  • decode(String s) - Accepts a String and decodes it into an Integer

The decode() method accepts decimal, hexadecimal and octal numbers, but doesn't support binary:

String string1 = "100";
String string2 = "50";
String string3 = "20";

int number1 = Integer.decode(string1);
int number2 = Integer.decode(string2); 
int number3 = Integer.decode(string3); 

System.out.println("Parsing String \"" + string1 + "\": " + number2);
System.out.println("Parsing String \"" + string2 + "\": " + number2);
System.out.println("Parsing String \"" + string3 + "\": " + number3);

Running this piece of code will yield:

Parsing String "100": 50
Parsing String "50": 50
Parsing String "20": 20

Conclusion

We've covered one of the fundamental topics of Java and common problem developers face - Converting a String to an Integer or int using tools shipped with the JDK.

Last Updated: April 24th, 2020
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms