- 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);}}chevron_rightfilter_noneOutput:
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);}}chevron_rightfilter_noneor
classGfG{publicstaticvoidmain(String args[]){String str3 = String.valueOf(1234);System.out.println("String str3 = "+ str3);}}chevron_rightfilter_noneOutput:
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);}}chevron_rightfilter_noneor
classGfG{publicstaticvoidmain(String args[]){intd =1234;String str4 =newInteger(d).toString();System.out.println("String str4 = "+ str4);}}chevron_rightfilter_noneor
classGfG{publicstaticvoidmain(String args[]){String str4 =newInteger(1234).toString();System.out.println("String str4 = "+ str4);}}chevron_rightfilter_noneOutput:
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);}}chevron_rightfilter_noneOutput:
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);}}chevron_rightfilter_noneOutput:
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);}}chevron_rightfilter_noneor
classGfG{publicstaticvoidmain(String args[]){String str6 =newStringBuffer().append(1234).toString();System.out.println("String str6 = "+ str6);}}chevron_rightfilter_noneOutput:
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);}}chevron_rightfilter_noneor
classGfG{publicstaticvoidmain(String args[]){String str7 =newStringBuilder().append(1234).toString();System.out.println("String str7 = "+ str7);}}chevron_rightfilter_noneOutput:
String str7 = 1234
- Convert with special radix
All of the examples above use the base (radix) 10. Following 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);}}chevron_rightfilter_noneOutput:
11111111
11111111 is the binary representation of the number 255.
Octal
classGfG{publicstaticvoidmain(String args[]){inti =255;String octalString = Integer.toOctalString(i);System.out.println(octalString);}}chevron_rightfilter_noneOutput:
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);}}chevron_rightfilter_noneOutput:
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);}}chevron_rightfilter_noneOutput:
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.
Attention reader! Donât stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Different ways for String to Integer Conversions in Java
- Number System and Base Conversions
- Sort a String in Java (2 different ways)
- Different Ways to Print First K Characters of the String in Java
- Different Ways to Remove all the Digits from String in Java
- Different Ways to Generate String by using Characters and Numbers in Java
- Program to Convert Set of Integer to Array of Integer in Java
- Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples
- Different ways of Method Overloading in Java
- Different ways of Reading a text file in Java
- 3 Different ways to print Exception messages in Java
- Different ways to create objects in Java
- Different Ways to Copy Content From One File to Another File in Java
- Different Ways To Check Java Version In Windows
- Integer.valueOf() vs Integer.parseInt() with Examples
- Program to count digits in an integer (4 Different Methods)
- Ways to express a number as product of two different factors
- Different ways to initialize a variable in C/C++
- Different ways to hide Action bar in Android with Examples
- 8 different ways to Add Two Numbers in C/C++

