Java allows you to convert values from one data type to another, under certain conditions. You can convert a string variable to a numeric value and a numeric value into a string variable. In this article, let’s check out how to convert int to String in Java.
Listed below are different ways to convert int to string in Java:
- How we convert int to String in Java: using Integer.toString(int)
- How we convert int to String in Java: using String.valueOf(int)
- How we convert int to String in Java: using String.format()
- How we convert int to String in Java: using DecimalFormat
- How we convert int to String in Java: using StringBuffer or StringBuilder
For each of these methods, I will be discussing the syntax, parameters involved and return value along with sample example programs in Java. Let’s get started.
How we convert int to String in Java?
Integer to String conversion in Java: using Integer.toString(int)
The Integer class has a static method that returns a String object representing the int parameter specified in the Integer.toString(int) function. This approach, unlike the others, can return a NullPointerException.
Syntax
There are two different expressions for Integer.toString() method:
- public static String toString(int i)
- public static String toString(int i, int radix)
Parameters
The parameters of this method are:
- i: the integer to be converted.
- radix: the used base – number system in order to represent the string.
The radix value is an optional parameter and if it is not set, for the decimal base system the default value is 10.
Return Value
The returned value for both expressions is a Java String that represents the integer “i” argument. If the radix parameter is used, the returned string is specified by the respective radix.
Convert Integer to String using Integer.toString().
package MyPackage; public class Method1 { public static void main(String args[]) { int n = Integer.MAX_VALUE; String str1 = Integer.toString(n); System.out.println("The output string is: " + str1); int m = Integer.MIN_VALUE; String str2 = Integer.toString(m); System.out.println("The output string is: " + str2); } }
The output string is: 2147483647 The output string is: -2147483648
Converting int to string using String.valueOf(int)
String.valueOf() is static utility method of String class that can convert most primitive data types to their String representation. It includes integers. This approach is considered as a best practice due to its simplicity.
Syntax
It is expressed as:
- public static String valueOf(int i)
Parameter
- i: the integer that needs to be converted
Return Value
This method returns the string representation of the int argument.
Convert Integer to String using String.valueOf().
class Method2 { public static void main(String args[]) { int number = 1234; String str = String.valueOf(number); System.out.println("With valueOf method: string5 = " + str); } }
Output
With valueOf method: string5 = 1234
Converting int to String using String.format()
In Java, String.format() is a new alternative method that can be used for converting an Integer to a String object. Though the purpose of this method is to format a string, it can also be used for conversion.
Syntax
There are two different expressions:
- public static String format(Locale l, String format, Object… args)
- public static String format(String format, Object… args)
Parameters
The arguments for this method are:
- l: the local to be addressed during the formatting
- format: the format string, which includes format specifier and sometimes a fixed text
- args: arguments that refer to the format specifiers set in the format parameter
Return Value
This method returns a formatted string, according to the format specifier and the specified arguments.
Convert int to String using String.format().
class Method3 { public static void main(String args[]) { int number = -1234; String str = String.format("%d", number); System.out.println("With format method: string = " + str); } }
Output
With format method: string = -1234
Converting int to String using DecimalFormat
DecimalFormat is a concrete subclass of NumberFormat class that formats decimal numbers. It has a lot of features designed to parse and format numbers. You can use it to format a number to a String representation following a certain pattern. Here’s an example program.
import java.text.DecimalFormat; public class Method4 { public static void main(String[] args) { int number = 12345; DecimalFormat numberFormat = new DecimalFormat("##,###"); String str = numberFormat.format(12345); System.out.println("The number to be converted is: " + number); System.out.println("The string version of 12345 is: " + str); } }
The number to be converted is: 12345 The string version of 12345 is: 12,345
If you know how to use DecimalFormat method, it is the best option for converting Integer to String, because of the level of control that you can have with the formatting. You can specify the number of decimal places and comma separator for better readability as shown in the above example.
Converting int to String using StringBuffer or StringBuilder
StringBuilder and StringBuffer are the classes used to concatenate multiple values into a single String. StringBuffer is thread safe but slower, whereas StringBuilder isn’t thread safe but is faster. Below I have given an example demonstrating these classes and explained how it works. Take a look.
Convert int to String using StringBuilder/StringBuffer.
class Method5 { public static void main(String args[]) { int number1 = -1234; StringBuilder sb = new StringBuilder(); sb.append(number1); String str1 = sb.toString(); System.out.println("With StringBuilder method: string = " + str1); StringBuffer SB = new StringBuffer(); SB.append(number1); String str2 = SB.toString(); System.out.println("With StringBuffer method: string = " + str2); } }
Output
With StringBuilder method: string = -1234 With StringBuffer method: string = -1234
The StringBuilder object represents a String object that can be modified and treated as an array with a sequence of characters. To add a new argument to the end of the string, StringBuilder instance implements the append() method. At the end it is important to call the toString() method, in order to take the string representation of the data. Alternatively, you can use the shorthand version of these classes as well. Here’s how to convert int to String in Java:
Example of converting int to String using StringBuilder/StringBuffer.
class Method6 { public static void main(String args[]) { String str1 = new StringBuilder().append(1234).toString(); System.out.println("With StringBuilder method: string = " + str1); String str2 = new StringBuffer().append(1234).toString(); System.out.println("With StringBuffer method: string = " + str2); } }
Output
With StringBuilder method: string = -1234 With StringBuffer method: string = -1234
The most important thing is to call the toString() method, in order to get the string representation of the data.
This brings us to the end of this ‘How to Convert int to String in Java’ article. I have covered one of the fundamental topics of Java, converting an int or Integer to a String using tools shipped with the JDK. Hope you are clear with all that has been shared with you in this article.
Make sure you practice as much as possible and revert your experience.
Check out the Java Certification Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.
Got a question for us? Please mention it in the comments section of this ‘How to Convert int to String in Java’ article and we will get back to you as soon as possible.