Categories

Sep 3, 2019 in JAVA

Q: convert int to string

1 Answer

Sep 4, 2019
Different ways for Integer to String Conversions In Java

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 :

filter_none

edit

play_arrow

brightness_4

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

Convert using String.valueOf(int)

Example :

filter_none

edit

play_arrow

brightness_4

class GfG

{

  public static void main(String args[])

  {

    int c = 1234;

    String str3 = String.valueOf(c);

    System.out.println("String str3 = " + str3);

  }

}  

or

filter_none

edit

play_arrow

brightness_4

class GfG

{

  public static void main(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 :

filter_none

edit

play_arrow

brightness_4

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

filter_none

edit

play_arrow

brightness_4

class GfG

{

  public static void main(String args[])

  {

    int d = 1234;

    String str4 = new Integer(d).toString();

    System.out.println("String str4 = " + str4);

  }

}  

or

filter_none

edit

play_arrow

brightness_4

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.

Convert using DecimalFormat

The class java.text.DecimalFormat is a class that formats a number to a String.

Example :

filter_none

edit

play_arrow

brightness_4

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

filter_none

edit

play_arrow

brightness_4

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.

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

filter_none

edit

play_arrow

brightness_4

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

filter_none

edit

play_arrow

brightness_4

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

filter_none

edit

play_arrow

brightness_4

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

filter_none

edit

play_arrow

brightness_4

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

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

filter_none

edit

play_arrow

brightness_4

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

filter_none

edit

play_arrow

brightness_4

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

filter_none

edit

play_arrow

brightness_4

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.

filter_none

edit

play_arrow

brightness_4

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
Click here to read more about JAVA
Click here to read more about Insurance

Related questions

Madanswer
Sep 3, 2019 in JAVA
Jun 16, 2019 in JAVA
Oct 20 in JAVA
...