Different ways for Integer to String Conversions In Java

2.3
  1. 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 :

    class GfG
    {
      public static void main(String args[])
      {
        int a = 1234;
        int b = -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
    
  2. Convert using String.valueOf(int)
    Example :

    class GfG
    {
      public static void main(String args[])
      {
        int c = 1234;
        String str3 = String.valueOf(c);
        System.out.println("String str3 = " + str3);
      }
    } 
    

    or

    class GfG
    {
      public static void main(String args[])
      {
        String str3 = String.valueOf(1234);
        System.out.println("String str3 = " + str3);
      }
    } 
    

    Output:

    String str3 = 1234
    
  3. Convert using Integer(int).toString()
    This methods uses instance of Integer class to invoke it’s toString() method.
    Example :

    class GfG
    {
      public static void main(String args[])
      {
        int d = 1234;
        Integer obj = new Integer(d);
        String str4 = obj.toString();
        System.out.println("String str4 = " + str4);
      }
    } 
    

    or

    class GfG
    {
      public static void main(String args[])
      {
        int d = 1234;
        String str4 = new Integer(d).toString();
        System.out.println("String str4 = " + str4);
      }
    } 
    

    or

    class GfG
    {
      public static void main(String args[])
      {
        String str4 = new Integer(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.

  4. Convert using DecimalFormat
    The class java.text.DecimalFormat is a class that formats a number to a String.
    Example :

    import java.text.DecimalFormat;
    class GfG
    {
      public static void main(String args[])
      {
        int e = 12345;
        DecimalFormat df = new DecimalFormat("#");
        String str5 = df.format(e);
        System.out.println(str5);
      }
    } 
    

    Output:

    String str5 = 12345
    

    or

    import java.text.DecimalFormat;
    class GfG
    {
      public static void main(String args[])
      {
        int e = 12345;
        DecimalFormat df = new DecimalFormat("#,###");
        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.

  5. 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 Example

    class GfG
    {
      public static void main(String args[])
      {
        int f = 1234;
        StringBuffer sb = new StringBuffer();
        sb.append(f);
        String str6 = sb.toString();
        System.out.println("String str6 = " + str6);
      }
    } 
    

    or

    class GfG
    {
      public static void main(String args[])
      {
        String str6 = new StringBuffer().append(1234).toString();
        System.out.println("String str6 = " + str6);
      }
    } 
    

    Output:

    String str6 = 1234
    

    StringBuilder Example

    class GfG
    {
      public static void main(String args[])
      {
        int g = 1234;
        StringBuilder sb = new StringBuilder();
        sb.append(g);
        String str7 = sb.toString();
        System.out.println("String str7 = " + str7);
      }
    } 
    

    or

    class GfG
    {
      public static void main(String args[])
      {
        String str7 = new StringBuilder().append(1234).toString();
        System.out.println("String str7 = " + str7);
      }
    } 
    

    Output:

    String str7 = 1234
    
  6. 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

    class GfG
    {
      public static void main(String args[])
      {
        int h = 255;
        String binaryString = Integer.toBinaryString(h);
        System.out.println(binaryString);
      }
    } 
    

    Output:

    11111111
    

    11111111 is the binary representation of the number 255.

    Ocatal

    class GfG
    {
      public static void main(String args[])
      {
        int i = 255;
        String octalString = Integer.toOctalString(i);
        System.out.println(octalString);
      }
    } 
    

    Output:

    377
    

    377 is the octal representation of the number 255.

    Hexadecimal

    class GfG
    {
      public static void main(String args[])
      {
        int j = 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.

    class GfG
    {
      public static void main(String args[])
      {
        int k = 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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.3 Average Difficulty : 2.3/5.0
Based on 27 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.