Java-Tutorials-5-Conversion Functions-String to Int

Convert using Integer.toString(int)

The Integer class has a static method that returns a String object representing the specified int parameter. Using this is an efficient solution.

Syntax

The argument i is converted and returned as a string instance. If the number is negative, the sign will be preserved.

Example

 

The code is equivalent to:

If you will try and search for solutions, this is one of the most popular ways of converting an int to String.

Convert using String.valueOf(int)

The String class has several static methods that can convert most primitive types to their String representation. This includes integers.

Example

or

This is also an efficient solution like the first option above. And because this is simple and efficient, it is also a very popular method for converting an int to String.

Convert using new Integer(int).toString()

Another alternative method is to create an instance of Integer class and then invoke it’s toString() method. This is a little less efficient than the first two options shown above. This is because a new instance of Integer is created before conversion is performed.

Example

int number = 782;

Integer intInstance = new Integer(number);

String numberAsString = intInstance.toString();

We can shortened to:

int number = 782;

String numberAsString = new Integer(number).toString();

or just:

String numberAsString = new Integer(-782).toString();

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.

Convert using String.format()

String.format() is a new alternative that can be used for converting an Integer to String object. It is first introduced in Java 5 (JDK1.5) and has many cool features.

Syntax

public static String format(String format, Object args)

There are many options on how to use this method. But for conversion purposes, here is a simple example:

Example

int number = 782;

String numberAsString = String.format (“%d”, number);

And the variable numberAsString will have the value “-782”

If you are using Java 5 or higher, this is also a very simple alternative for converting an int to String object.

 

Convert using DecimalFormat

The class java.text.DecimalFormat is a class that formats a number to a String representation following a certain pattern.
Example

Will output

12345

This is an example that will convert to String but with commas

Will output

12,345

This is my favorite from all the options shown in this post because of the level of control that you can do with the formatting. 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. These two classes can be used to convert a Java Integer to String.

StringBuffer Example

StringBuilder Example

Shortened Examples

String numberAsString = new StringBuffer().append(-782).toString();

String numberAsString = new StringBuilder().append(-782).toString();

Quick Solution

If you want something simple that works but not elegant, you can use this code:

int number = 782;

String numberAsString = “” + number;

When you concatenate a String and an integer, the result is a String. Internally, the code is inefficient because there are intermediate objects that are created behind the scene.

Convert with special radix

All of the examples above uses the base (radix) 10. There are cases when we wish to convert a Java Integer to String but using another base. There are convenient methods to convert to binary, octal, and hexadecimal system. Arbitrary custom number system is also supported.

Examples

  • Binary

    The output is

  • 11111111
  •  

    11111111 is the binary representation of the number 255.

  • Octal
  • The output is

  • 377
  •  

    377 is the octal representation of the number 255.

  • Hexadecimal
  • The output is

  • ff
  •  

    ff is the hexadecimal representation of the number 255.

  • Custom Base/Radix
    We can use any other custom base/radix when converting an int to String. The sample shown below uses the base 7 number system.

    The output is

  • 513
  •  


    513 is the representation of the number 255 when written in the base 7 system.