Sometimes while we are working with strings, we need to convert that string value into an integer value. Suppose we have a number in string form, and now we want to perform a mathematical operation on it. While it is a string, we cannot perform any mathematical operation on it, and if we try to do that, it will throw an error. To do this task, we need to convert a string into an integer first, and then we can perform any operation we want. There are different ways to convert a string to an integer in Java.
- parseInt()
- valueOf()
- decode()
Table of Contents
parseInt() Method
The parseInt() Method belongs to the integer class of the Java.lang package. It converts a string to a primitive int value. There are three types of parseInt() Method and that are:
- parseInt(String s): This Method returns the integer value, represented by the argument in decimal equivalent.
- parseInt(String s, int radix): This Method returns the integer value, represented by the string argument in the specified radix.
- parseInt(CharSequence s, int beginIndex, int endIndex, int radix): This Method returns the integer value, represented by the sequence of characters, inclusive and exclusive value argument in the specified radix.
Let’s look at an example in which we will use three of these methods of parseInt() Method.
Example Code: IntegerParseInt.java
public class IntegerParseInt {
public static void main(String[] args) {
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);
}}
Output

valueOf() Method
The valueOf() method takes the input value and convert/parse it into an integer. It also has three types:
- valueOf(String s): This method accepts string and converts it into an integer.
- valueOf(int i): This method accepts int value and converts it into an integer.
- valueOf(String s, int radix): This method accepts a string and converts into an integer value on the basis of radix.
Let’s have a look on an example.
Example Code: ValueOfMethod.java
public class ValueOfMethod{
public static void main(String args[]){
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);
}}
Output

decode() Method
The decode() Method accepts only one parameter, and it has one type that is decode(String s). It accepts a string and decodes it into an integer.
Example Code: DecodeMethod.java
public class DecodeMethod{
public static void main(String args[]){
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);
}}
Output

Conclusion
In conclusion, we discussed the conversion string to integer in java programming. We saw three different methods to perform this task with their coding example to understand it more clearly. The methods we discussed were parseInt(), valueOf(), and decode() Method; we used these three methods to convert the string value into an int value.