String To Integer And Integer To String Conversion In Java


In most of time, user gives his input through either textfield or textarea. The input user enters via textfield or textarea is always in the string format. You often need this input in the integer form. For example – age, mobileNo etc. To make the conversion easy from string to integer or integer to string, Java provides some useful and easy to use methods. In this post, we will discuss different methods to convert string to integer and also from integer to string.

How To Convert String To Integer In Java?

There are two methods available in java to convert string to integer. One is Integer.parseInt() method and another one is Integer.valueOf() method. Both these methods are static methods of java.lang.Integer class. Both these methods throw NumberFormatException if input string is not a valid integer. The main difference between Integer.parseInt() and Integer.valueOf() method is that parseInt() method returns primitive int where as valueOf() method returns java.lang.Integer object.

Java Program To Convert String To Integer Using Integer.parseInt() method :

public class StringToInteger
{
	public static void main(String[] args) 
	{
		String s = "2015";
		
		int i = Integer.parseInt(s);
		
		System.out.println(i);          //Output : 2015
	}
}

Java Program To Convert String To Integer Using Integer.valueOf() method :

public class StringToInteger
{
	public static void main(String[] args) 
	{
		String s = "2015";
		
		int i = Integer.valueOf(s);
		
		System.out.println(i);          //Output : 2015
	}
}

How To Convert Integer To String In Java?

You are also often need to do the reverse conversion i.e converting from integer to string. Java provides couple of methods to do that also. one is Integer.toString() method and another one is String.valueOf() method. Both these methods return string representation of the given integer.

Java Program To Convert Integer To String Using Integer.toString() Method :

public class IntegerToString
{
	public static void main(String[] args) 
	{
		int i = 2015;
		
		String s = Integer.toString(i);
		
		System.out.println(s);     //Output : 2015
	}
}

Java Program To Convert Integer To String Using String.valueOf() method :

public class IntegerToString
{
	public static void main(String[] args) 
	{
		int i = 2015;
		
		String s = String.valueOf(i);
		
		System.out.println(s);     //Output : 2015
	}
}

5 Comments

    • A variable of primitive data type is often passed by value, not by reference. Quiet often there might arise the need to consider such variable of primitive data types as reference types. The value returned by Integer.valueOf() method is of Integer type. The type Integer act as wrapper class. A wrapper class is one which is used to wrap the data inside it in order to use the data as an object representation.

      SO when you need to pass the integer value as an object use Integer.valueOf() and when you simply wanna use the primitive value then use Integer.parseInt()..
      Hope it helps…

  1. I have one doubt that if valueOf() method returns object then can we write it as
    Integer i = Integer.valueOf(s)
    if yes then how
    int i = Integer.valueOf(s) allows us how is it possible to convert store object int int pdt.

    please anyone help me.

    • This is called auto boxing, introduced in java 5. So you need not to create reference of Integer jvm will do it for you.

Leave a Reply