To convert int to String, use either String.valueOf()
or Integer.toString()
method.
1. Convert int to String – String.valueOf()
String.valueOf(int i) method returns the string representation of the int argument. The representation is exactly the one returned by the Integer.toString()
method of one argument.
1.1. Syntax
/** * @param i - an int value. * @return a - string representation of the int argument. */ public static String valueOf(int i)
1.2. int to String Example
int countInt = 40; String countStr = String.valueOf( countInt );
2. Convert int to String – Integer.toString()
Integer.toString(int i) method Returns a string object representing the specified integer passed as method argument. By default, argument is converted to signed decimal (radix 10) in string format.
2.1. Syntax
/** * @param i - an int value. * @return a - string representation of the int argument in base 10 */ public static String toString(int i)
2.2. int to String Example
int countInt = 40; String countStr = Integer.toString( countInt );
3. Convert Integer to String
To convert Integer
object to String, simply call method toString() on integer object.
Integer year = new Integer(2018); String yearString = year.toString();
4. Java example to convert int to String value
This example shows how to use above both methods i.e. String.valueOf() and Integer.toString() to convert a given integer value to string value.
In second part, it gives example to convert Integer object to String representation.
public class StringExample { public static void main(String[] args) { // 1. Converting int value to String value int intParam = 1001; String strValue1 = String.valueOf(intParam); String strValue2 = Integer.toString(intParam); // 2. Converting Integer object to String value Integer integerParam = new Integer(2018); String strValue3 = String.valueOf(integerParam); String strValue4 = integerParam.toString(); //Verify results System.out.println(strValue1); System.out.println(strValue2); System.out.println(strValue3); System.out.println(strValue4); } }
Program Output:
1001 1001 2018 2018
Checkout this example to convert String to int values.
Happy Learning !!