Core JAVA

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.

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago