dineshonjava

Convert String to Integer to String in Java with Example

In java Integer is a wrapper class of int. As of Java 5, java provide automatic conversion to int to Integer and Integer to int, it also called Auto-boxing feature of java. But converting String to Integer and Integer to String is not an auto-boxing. Remember auto-boxing only converts primitive to Object it doesn't convert one data type to other. Converting String to Integer and Integer to String is one of the basic tasks of java. There are many use cases where we need of this conversion like in web application any request parameter with query string is always in form string whatever it is int or long. So its most useful in web application to converting String to Integer and also because of its frequent need given that String and Integer are two most widely used type in all sort of program and you often gets data between any of these formats.

So it is not only for converting Integer to String and String to Integer, It conversion may be for Long, Date, Enum, Double to String ,but it is similar to converting String to Integer.


Converting String to Integer in Java

There are following different ways of converting String object into int or Integer in Java :

By using Intger.parseInt(String value) method
This is method in Integer class used to converting string value to int value. This is recommended method to be used for string to int conversion. It is extremely easy and most flexible way of converting String to Integer. Let see an example of String to int conversion:

//using Integer.parseInt
 String str = "100"
 int i = Integer.parseInt(str);
 System.out.println("i: " + i);

Suppose str is not a proper number then this method Integer.parseInt() will throw NumberFormatException. Other wrapper classes in java has same technique to be used to convert other data type like float and Double to String in Java. It is static method in the all wrapper classes like Float.parseFloat() and Double.parseDouble() to perform data type conversion.


By Using Integer.valueOf(String value) method

Integer class provide another method to converting string to integer value. This method return an Integer object holding the value represented by the string argument. This is an example of Factory method design pattern in Java and known as Integer.valueOf(), this is also a static method like main and can be used as utility for string to int conversion.

Let’s see an example of using Integer.valueOf() to convert String into int in java.

//using Integer.valueOf()
 String str = "100"
 Integer  i = Integer.valueOf(str);
 System.out.println("i: " + i);

Suppose str is not a proper number then this method Integer.parseInt() will also throw NumberFormatException. Integer.valueOf() and Integer.parseInt(str) both are using for converting String to Integer value but there is a interesting difference between them, static valueOf() method is that it is used to create instance of wrapper class during Auto-boxing in Java So we careful whenever you want compare the value of integer after conversion with valueOf() method , it can cause subtle issues while comparing primitive to Object e.g. int to Integer using equality operator (==), because caches Integer instance in the range -128 to 127. Learn more about it.

Converting Integer to String in Java

In the above we have seen how to convert String to Integer value but here we are going explain how to convert Integer value to String value.

Adding Empty String with Integer

String str = 100 + "";

We can concatenate any number with empty String and it will create a new String. It is very simple way to convert int value to String object. Here we used "+" concatenation operator with String to convert int variable into String object.

It is very simple way but it is also one of the most poor way of converting Integer value to String. Because it is actually soil you code. When you write like String str = 100 + "" then your code translated by JVM as below:

new StringBuilder().append(100).append("").toString();

In java constructor of StringBuilder() constructs a string builder with no characters in it and an initial capacity of 16 characters. So we can append characters up to to 16 length to that StringBuilder, if appending more than 16 character will expand StringBuilder buffer. At the end, StringBuilder.toString() will create a new String object with a copy of StringBuilder buffer. That is why, it is not better way to converting Integer value to String by using empty string concatenation. For this simple way of converting a single integer value to String you will need to allocate one StringBuilder, one char array char[16], one String and one char[] of appropriate size to fit your input value. For avoiding this nonsense flow simply use String.valueOf() method because this method will not only benefit from cached set of values but also you will at least avoid creating a StringBuilder.

String price = String.valueOf(100);

Read More about StringBuffer and StringBuilder.