Beginners Programs :

Convert String To int Java – Tutorial & Examples

How to Convert String to int in Java – The following article will let you know the complete information about string to int java, in case if you have any doubts do let me know.

We have different ways to convert a String to int in Java

  • Integer.parseInt()
  • Integer.valueOf()
  • Integer.parseUnsignedInt()

Our own logic equivalent to – Integer.parseInt()

  • Integer.decode()
  • Integer() constructor


String To Int Java

Integer.parseInt() – How It Works:

Syntax1 – int parseInt(String);


It converts the given String into an integer. Suppose we give a String “234” as argument to the method, then it returns the integer 234. As this is a static method, we can invoke this using its class name.


Algorithm for converting string to int without built-in method:



Integer.decode() – How It Works

An empty String or a String that contains non-digits will result in Number Format Exception.


“234A”, “34.54”, “smile”, “”, etc.

Syntax2:               int parseInt(String,int);

This is similar to normal parseInt(), but it takes an integer as second argument and this argument represents the radix of the content in the input String.

Suppose we want to convert an octal number (which is in String format) into its equivalent decimal integer then the second argument should be 8.

Similarly, if we want a hexa-decimal String to its decimal number then the second argument should be 16.

Eg:            Integer.parseInt(“234”,10);  will result in 234

                  ===>  2*(10^2)+3*(10^1)+4*(10^0)

                  ===>  (2*100)+(3* 10)+(4*1)

                  ===>  200+30+4

                  ===>    234



 Integer.parseInt(“234”,2);  will result in NumberFormatException as allowed digits in binary(2) number                        system are 0 and 1 only.

 Integer.parseInt(“789”,8);  will result in NumberFormatException as allowed digits in octal(8) number                           system are from 0 to 7 only

string to int java – Integer.valueOf()

It can also be used similar to Integer.parseInt() to convert a String to int. This method also has an overloaded method to take an int (radix) as second argument where as the first argument is a String.

Integer.parseInt() returns the result as a basic (primitive) int type where as Integer.valueOf() returns the result an Integer object.


  • Integer.valueOf(“234”); will give 234 as Integer object
  • Integer.valueOf(“412”); will give 412 as Integer object
  • Integer.valueOf(“234”,8); will give 156 as Integer object
  • Integer.valueOf(“234”,16); will give 564 as Integer object


This is also similar to Integer.valueOf() and Integer.parseInt() but it can work on positive values only.


  • Integer.parseUnsignedInt(“874”); returns 874         
  • Integer.parseUnsignedInt(“+874”); returns 874          
  • Integer.parseUnsignedInt(“-874”); raises NumberFormatException


Syntax: Integer decode(String);

This method converts the given string into an int and returns an Integer object.


The decode() method can assume the type (decimal, octal, hexadecimal…) of the value and makes the conversion accordingly. In computer parlance, a number preceded by 0 is treated as octal and a number


Convert String To int java – Integer()

The class Integer has several constructor and one of them receives a String as argument. By using this, we can convert a String to Integer object. Once we have an integer object, we can convert it to an int (primitive) type.


If you have any doubts related to Convert Java String To Int do leave a comment here.

Leave a Reply

Your email address will not be published. Required fields are marked *