Java – Convert Int to String

You can convert an int to string in Java using String class methods.

In this tutorial, we shall go through some of the ways to convert an int to string, with example Java programs.

Int to String using Integer.toString() method

toString() is a static method in Integer class that returns string object representing the specified integer value. We have to pass the integer value as argument to toString() method.

In the following example we shall convert an int value (either true or false) to string by using Integer.toString() method.

Java Program

/**
 * Java Program - Convert Int to String
 */

public class IntToString {

	public static void main(String[] args) {
		String str = Integer.toString(38426);
		System.out.println(str);
		
		str = Integer.toString(-938426);
		System.out.println(str);
	}
}

Output

38426
-938426

Int to String using String Concatenation

This is the most easiest way to convert an integer to a string. All you have to do is add an empty string to it.

In the following example we shall convert an integer to string by concatenating the number to an empty string.

Java Program

/**
 * Java Program - Convert Int to String
 */

public class IntToString {

	public static void main(String[] args) {
		int n = 5239;
		
		//convert int to string using string concatenation
		String str = n + "";
		System.out.println(str);
	}
}

Run the above program and you shall get the following output.

Output

5239

Int to String using String.valueOf() method

We can use String.valueOf() method to get a string value from an integer. Pass the integer as argument to String.valueOf() function, and the function returns a String object.

In the following example we shall convert an integer to string by using String.valueOf() function.

Java Program

/**
 * Java Program - Convert Int to String
 */

public class IntToString {

	public static void main(String[] args) {
		int n = 5239;
		
		//convert int to string using String methods
		String str = String.valueOf(n);
		System.out.println(str);
	}
}

Int to String using StringBuffer.append() method

Create a StringBuilder object and append the integer value to it using append() method. append() method appends the string representation of the boolean value to this sequence. After append() function, call toString() method on the StringBuilder object, it returns string.

You can create StringBuilder object and call append() function and toString() function as chain in a single statement.

In the following example we shall convert a integer to string by using StringBuilder class.

Java Program

/**
 * Java Program - Convert Int to String
 */

public class IntToString {

	public static void main(String[] args) {
		String str = (new StringBuffer()).append(2638).toString();
		System.out.println(str);
		
		str = (new StringBuffer()).append(-8674).toString();
		System.out.println(str);
	}
}

Output

2638
-8674

Conclusion

In this Java Tutorial, we learned how to convert or typecast an int to string in Java, using String concatenation or String.valueOf() method.