In this tutorial, we will see how to convert int to String in Java. There are multiple use cases, where we may need to convert int to String. Let’s look at the options to perform this conversion.
1. Convert int to String in Java using String.valueOf()
The String.valueOf()
provides an option to pass int or Integer as an input and method will return the String output.
public class ConvertIntToString {
public static void main(String[] args) {
int input1 = 200;
Integer input2 = 400;
/**
* Convert int to String using String.valueOf.
*/
String output = String.valueOf(input1);
System.out.println(output); //200
/**
* Convert Integer object to String using
* String.valueOf() method.
*/
String output1 = String.valueOf(input2);
System.out.println(output1); // 400
String sampleOutput = new StringBuilder("Our output is ")
.append(output).append(" and ")
.append(output1)
.toString();
System.out.println(sampleOutput); //Our output is 200 and 400
}
}
Please keep in mind the following important points:
- The
String.valueOf()
for int will internally callInteger.toString(i)
for the conversion. - For the Integer Wrapper class, system will reply on the
toString
method for string conversion.
If efficiency is your primary concern and you have the Integer class, use Integer.toString(i).
2. Convert int to String using Integer.toString()
The Integer.toString()
takes Integer and convert to String. Please note that it will not work on the primitive type and we need to convert it to Integer before passing it to the Integer.toString()
method.
public class ConvertIntToString {
public static void main(String[] args) {
int input1 = 200;
Integer input2 = 400;
/**
* Convert int to String using String.toString.
* We are converting the primitive type to
* Integer before conversion
*/
String output = Integer.toString(Integer.valueOf(input1));
System.out.println(output); //200
/**
* Convert Integer object to String using
* String.toString method.
*/
String output1 = Integer.toString(input2);
System.out.println(output1); // 400
String sampleOutput = new StringBuilder("Our output is ")
.append(output).append(" and ")
.append(output1)
.toString();
System.out.println(sampleOutput); //Our output is 200 and 400
}
}
3. Convert int to String using StringBuffer or StringBuilder
As an alternate, we can use StringBuffer
or StringBuilder
to convert int to String in Java. This method will not work on the primitive type and we need to convert it to Integer.
public class ConvertIntToString {
public static void main(String[] args) {
int input1 = 200;
Integer input2 = 400;
//converted the int to Integer
Integer boxedInput = new Integer(input1);
String sampleOutput = new StringBuilder("Our output is ")
.append(input2).append(" and ")
.append(boxedInput)
.toString();
System.out.println(sampleOutput); //Our output is 200 and 400
}
}
4. Convert using String.format() method
This is not a direct way for the conversion but we can use String.format("%d", int)
for our conversion. Let’s look at some points before we look at the example.
- The first part of the string format used for string formatting
- Second parameter is the value which needs to be formatted in String.
public class ConvertIntToString {
public static void main(String[] args) {
int input1 = 200;
Integer input2 = 400;
String output = String.format("%d", input1);
System.out.println(output); //200
String output1 = String.format("%d", input2);
System.out.println(output1); //400
}
}
If you are looking for more complicated conversion using String.format() method, look at java.util.Formatter. You can also use the simple String concatenation for the conversion, but keep in mind that the String concatenation is not meant for the data conversion. To do this, add an empty String with the input.Let’s look at a sample example.
String output =""+400; //not recommended
Summary
In this article, we saw how to convert int to String in Java. We saw the following 4 method for converting int to String.
- Convert int to String using
String.valueOf()
method. - Using
Integer.toString()
. - Convert using
StringBuffer
andStringBuilder
. - Use
String.format()
to convert int in to String.
Follow our Java articles to get more in depth knowledge.