Int To String Java



Java Convert String To Integer

Different ways for Integer to String Conversions In Java

  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. Following 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.

Recommended Posts:

Improved By : Akanksha_Rai

Gallery Int To String Java

Python String To Int Int To String Journaldev

Java Io Stringwriter Class In Java Geeksforgeeks

How To Implement Your Own Inetaddress Isreachable String

6 Ways To Convert Java Int Float Double To String With 9

Solved Write A Java Program To Convert An Integer To Roma

4 Ways To Convert Int Value To String In Java Examples Java67

Java Example On How To Convert String To Boolean

How To Convert Int To String 3 Method In Java Netbeans

Java Long Parseunsignedlong String S Int Radix Method Example

Screen Shot Of Qanswer For Java Integer To String

Showinputdialog Conversion String To Int

Java Convert String To Int Examples

How To Easily Convert String To Integer In Java

How To Convert Float To Int In Java Examples Java67

The Do S And Don Ts Of Java Strings You Should Know

How To Convert Java Array To String Java Arrays Tostring

Python String To Int Int To String Journaldev

How To Convert Int To String In Java The Fastest Way

How To Convert String To Double In Java With Example Java67

Convert String To Int In Java Devcubicle

Convert A String To An Integer In Java Challenge Part 1 3

Solved Baseballgame Java Public Class Baseballgame Priv

How To Convert Int To String In Java The Fastest Way

Solved Rec Recording Java Public Class Recording Privat

2 Ways To Parse String To Int In Java Java67

Java Basics Java Programming Tutorial

Java Convert String To Int Javatpoint

In Java How To Move All 0 S To End Of Array Preserving Order


Comments