Convert an int to a String in java

How to convert an int to a String in java?

Integer to String conversion can be done using a number of options. Let us look at some of them.

String.valueOf(int)

String class provides utility methods to convert an int to a String. String.valueOf(int) method is very widely used and is considered a good practice for conversion.

Declaration
public static String valueOf(int i);
Simple Example.
package com.programtalk.learn.java.int2string;

public class Int2String {
	public static void main(String[] args) {
		int intValue = 1234;
		String stringValue = String.valueOf(intValue);
		System.out.println(stringValue); // prints 1234
	}
}
Note about Integer wrapper class conversion

If you pass a Integer wrapper class to valueOf method, you would be invoking a different method, Integer.valueOf(Object). One thing that you need to know about valueOf(Object) is that if you  pass a null Integer wrapper object, the string returned by this method would be "null". See below code snippet.

   Integer intValue = null;
   String stringValue = String.valueOf(intValue);
   System.out.println(stringValue); // prints null

 

Integer.toString(int)

This is also one of the popular ways of conversion and is also considered as a best practice for conversion. If you look at the implementation of String.valueOf(int) that internally calls Integer.toString(int) method.  This method accepts a parameter of primitive int type and returns back the String.

Declaration
public static String toString(int i)
Simple Example
package com.programtalk.learn.java.int2string;

public class Int2StringIntegerToString {
	public static void main(String[] args) {
		int intValue = 1234;
		String stringValue = Integer.toString(intValue);
		System.out.println(stringValue); // prints 1234
	}
}

Using StringBuilder

StringBuilder provides the concatenation operation for all the primitive types. The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. We will be using StringBuilder.append(int i) method. StringBuilder is not threadsafe. It was introduced in java 1.5 so as avoid synchronized operations of StringBuffer in singlethreaded environment.

Declaration
StringBuilder append(int i);
Simple Example
int intVal= 1234; 
StringBuilder stringBuilder = new StringBuilder(); 
stringBuilder.append(intVal); 
String stringValue = stringBuilder.toString();

Using StringBuffer

StringBuffer provides the concatenation operation for all the primitive types. The difference to the StringBuilder is that it is thread safe.

Declaration
StringBuffer append(int i);
Simple Example
int intVal= 1234; 
StringBuffer stringBuffer = new StringBuffer(); 
stringBuffer.append(intVal); 
String stringValue = stringBuffer.toString();

Using + Operator

An int can also be converted to String using a + operator.

int intValue = 123;
String stringValue = "" + intValue;

And the compiler internally will convert it to something like below.

String stringValue = new StringBuilder().append("").
                             append(intValue).toString()
Performance Issues

This seems to be fine. But when you are doing it in a loop, it becomes an issue,  as shown below

 String strValue = "";
 for(int i=0; i < 10000;i++){
   strValue += i;
 }

The above loop would be a performance drag. The compiler would end up creating StringBuilder object for every iteration of the loop and degrading the performance. See here for perforamce results. So avoid using such kind of concatenations in loop. You can do it by using StringBuilder as shown below but be aware that StringBuilder is not thread safe but its performance is better in single-threaded environment. StringBuffer is a thread safe option.

StringBuilder strBuilder = new StringBuilder();
for(int i=0; i < 10000; i++) {
  strBuilder.append(i);
}
String stringValue = strBuilder.toString();

MessageFormat.format()

A clean way of appending int to strings.


String str = MessageFormat.format("I am an int Value {0}", 2);

{0} defines the placeholder for the argument at 0 position which is an int here.

String.format()

This is a very clean way of appending int to strings.


String stringValue = String.format("This is an int value %d", 5)

The %d symbol is a placeholder for decimal value (e.g. an int or long in Java) . The parameter will be appended to the string in place of %d symbol.

 

Summary

So in this tutorial we saw how to convert int to String. I hope that has helped you. Just drop a note in the below comment section in case of questions or comments.

You also download the source code at github.

Like this post? Don’t forget to share it!

4 Comments

Leave a Reply

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