- Convert using Integer.toString(int)
The Integer class has a static method that returns a String object representing the specified int parameter.
Syntax :public static String toString(int i)
The argument i is converted and returned as a string instance. If the number is negative, the sign will be preserved.
Example :classGfG{publicstaticvoidmain(String args[]){inta =1234;intb = -1234;String str1 = Integer.toString(a);String str2 = Integer.toString(b);System.out.println("String str1 = "+ str1);System.out.println("String str2 = "+ str2);}}Output:
String str1 = 1234 String str2 = -1234
- Convert using String.valueOf(int)
Example :classGfG{publicstaticvoidmain(String args[]){intc =1234;String str3 = String.valueOf(c);System.out.println("String str3 = "+ str3);}}or
classGfG{publicstaticvoidmain(String args[]){String str3 = String.valueOf(1234);System.out.println("String str3 = "+ str3);}}Output:
String str3 = 1234
- Convert using Integer(int).toString()
This methods uses instance of Integer class to invoke it’s toString() method.
Example :classGfG{publicstaticvoidmain(String args[]){intd =1234;Integer obj =newInteger(d);String str4 = obj.toString();System.out.println("String str4 = "+ str4);}}or
classGfG{publicstaticvoidmain(String args[]){intd =1234;String str4 =newInteger(d).toString();System.out.println("String str4 = "+ str4);}}or
classGfG{publicstaticvoidmain(String args[]){String str4 =newInteger(1234).toString();System.out.println("String str4 = "+ str4);}}Output:
String str4 = 1234
If your variable is of primitive type (int), it is better to use Integer.toString(int) or String.valueOf(int). But if your variable is already an instance of Integer (wrapper class of the primitive type int), it is better to just invoke it’s toString() method as shown above.
This method is not efficient as instance of Integer class is created before conversion is performed. - Convert using DecimalFormat
The class java.text.DecimalFormat is a class that formats a number to a String.
Example :importjava.text.DecimalFormat;classGfG{publicstaticvoidmain(String args[]){inte =12345;DecimalFormat df =newDecimalFormat("#");String str5 = df.format(e);System.out.println(str5);}}Output:
String str5 = 12345
or
importjava.text.DecimalFormat;classGfG{publicstaticvoidmain(String args[]){inte =12345;DecimalFormat df =newDecimalFormat("#,###");String Str5 = df.format(e);System.out.println(Str5);}}Output:
String str5 = 12,345
Using this method, you can specify the number of decimal places and comma separator for readability.
- Convert using StringBuffer or StringBuilder
StringBuffer is a class that is used to concatenate multiple values into a String. StringBuilder works similarly but not thread safe like StringBuffer.
StringBuffer ExampleclassGfG{publicstaticvoidmain(String args[]){intf =1234;StringBuffer sb =newStringBuffer();sb.append(f);String str6 = sb.toString();System.out.println("String str6 = "+ str6);}}or
classGfG{publicstaticvoidmain(String args[]){String str6 =newStringBuffer().append(1234).toString();System.out.println("String str6 = "+ str6);}}Output:
String str6 = 1234
StringBuilder Example
classGfG{publicstaticvoidmain(String args[]){intg =1234;StringBuilder sb =newStringBuilder();sb.append(g);String str7 = sb.toString();System.out.println("String str7 = "+ str7);}}or
classGfG{publicstaticvoidmain(String args[]){String str7 =newStringBuilder().append(1234).toString();System.out.println("String str7 = "+ str7);}}Output:
String str7 = 1234
- Convert with special radix
All of the examples above use the base (radix) 10. Follwing are convenient methods to convert to binary, octal, and hexadecimal system. Arbitrary custom number system is also supported.
Examples :Binary
classGfG{publicstaticvoidmain(String args[]){inth =255;String binaryString = Integer.toBinaryString(h);System.out.println(binaryString);}}Output:
11111111
11111111 is the binary representation of the number 255.
Ocatal
classGfG{publicstaticvoidmain(String args[]){inti =255;String octalString = Integer.toOctalString(i);System.out.println(octalString);}}Output:
377
377 is the octal representation of the number 255.
Hexadecimal
classGfG{publicstaticvoidmain(String args[]){intj =255;String hexString = Integer.toHexString(j);System.out.println(hexString);}}Output:
ff
ff is the hexadecimal representation of the number 255.
Custom Base/Radix
you can use any other custom base/radix when converting an int to String.
Following example uses the base 7 number system.classGfG{publicstaticvoidmain(String args[]){intk =255;String customString = Integer.toString(k,7);System.out.println(customString);}}Output:
513
513 is the representation of the number 255 when written in the base 7 system.
This article is contributed by Amit Khandelwal .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Counting number of lines, words, characters and paragraphs in a text file using Java
- Java.util.StringJoiner in Java8
- Reverse a string in Java
- Searching characters and substring in a String in Java
- Arrays in Java
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- Split() String method in Java with examples
- Swap two Strings without using third user defined variable in Java
- String to Integer in Java - parseInt()
- Arrays.sort() in Java with examples
- Ways to read input from console in Java
- How to Initialize and Compare Strings in Java?
- String class in Java | Set 1
- Scanner Class in Java
- String vs StringBuilder vs StringBuffer in Java


