In this guide, we will learn how to convert an int to string in Java. We can convert int to String using String.valueOf() or Integer.toString() method. We can also use String.format() method for the conversion.
1. Convert int to String using String.valueOf()
String.valueOf(int i) method takes integer value as an argument and returns a string representing the int argument.
Method signature:
public static String valueOf(int i)
parameters:
i – integer that needs to be converted to a string
returns:
A string representing the integer argument
Java – int to String using String.valueOf()
public class JavaExample { public static void main(String args[]) { int ivar = 111; String str = String.valueOf(ivar); System.out.println("String is: "+str); //output is: 555111 because the str is a string //and the + would concatenate the 555 and str System.out.println(555+str); } }
Output:
2. Convert int to String using Integer.toString()
Integer.toString(int i) method works same as String.valueOf(int i) method. It belongs to the Integer class and converts the specified integer value to String. for e.g. if passed value is 101 then the returned string value would be “101”.
Method signature:
public static String toString(int i)
parameters:
i – integer that requires conversion
returns:
String representing the integer i.
Example:
int ivar2 = 200; String str2 = Integer.toString(ivar2);
Java – int to String using Integer.toString()
public class Example { public static void main(String args[]) { int ivar = 111; String str = Integer.toString(ivar); System.out.println("String is: "+str); //output is: 555111 because the str is a string //and the + would concatenate the 555 and str System.out.println(555+str); //output is: 666 because ivar is int value and the //+ would perform the addition of 555 and ivar System.out.println(555+ivar); } }
Output:
String is: 111 555111 666
Example: Converting int to String
This program demonstrates the use of both the above mentioned methods(String.valueOf() and Integer.toString()). Here we have two integer variables and we are converting one of them using String.valueOf(int i) method and other one using Integer.toString(int i) method.
public class IntToString { public static void main(String[] args) { /* Method 1: using valueOf() method * of String class. */ int ivar = 111; String str = String.valueOf(ivar); System.out.println("String is: "+str); /* Method 2: using toString() method * of Integer class */ int ivar2 = 200; String str2 = Integer.toString(ivar2); System.out.println("String2 is: "+str2); } }
Output:
String is: 111 String2 is: 200
3. String.format() method for conversion
public class JavaExample{ public static void main(String args[]){ int num = 99; String str = String.format("%d",num); System.out.println("hello"+str); } }
Output:
hello99
This is very well presented instruction. One possible addition would be adding explanation of why one might want to do (the example). IE why would I want to convert a integer to a string? Probably a simple reason but none come to mind besides I could do it.