Available methods in Java for int to string conversion

In Java, you may use a number of ways to convert numbers (an int, float, double etc.) to string. Following methods can be used for conversion:

The preferred and general ways for conversion in Java:

  • toString()
  • valueOf
  • String concatenation operator (+)

Other ways of converting numbers to string:

  • format()
  • StringBuffer
  • StringBuilder
Converting by toString() example valueOf example StringBuilder Example

Each of these ways is explained below with examples for converting numbers (int or float and double) to string.

Java integer to string conversion by Integer.toString() method

The toString() method of Integer class can be used for converting a primitive int or Integer object into a string. This is how you may use this method:

For int to string:

toString(int i)

For converting integer object to the string:

Integer.toString(Integer_Object)

See the following code for using toString in both ways:

See online demo and code

The output of above code is:

Java int to string

You saw, an int and the other Integer type variables declared. Those are assigned to the string variables after converting to strings by using the valueOf method.

Converting float and double to string by toString method

Similarly, you may use the toString method of the Float class for converting a primitive float or object float to string.

See the following example where a float (primitive) and Float object are changed to string by using the valueOf method:

The output:

Atter float to stirng = 50.55

Atter Float to stirng = 66.45

Converting double to string example

The Double class also has toString() method that can be used for converting the double (primitive type) and Double objects into the string. See a demonstration below:

The output of above example is:

Atter double to stirng = 50.55

Atter Double to stirng = 15.5

Using String valueOf() method for int to string conversion

The other preferred method for converting numbers to string is the String class valueOf() method. By using the String valueOf() method, you may convert any of these:

  • Primitive int to string
  • Integer object to string
  • Primitive long to string type
  • Long object to string type
  • Primitive float to string
  • Float object to string
  • double to string
  • Double object to string etc.

See the following example where all these data types are used for converting into strings:

See online demo and code

The output:

int to string valueOf

An example of concatenation operator (+) for conversion

The plus sign (+) is a string concatenation operator and that is also used for conversion of objects to strings. Java provides special support for this operator.

You may use the + operator for converting integers or other numbers to strings as well. This is the simplest way of accomplishing this task.

See the following example where a string is created based on values of an int, float and a double variable by using the + operator:

The Output:

a String: 77  24.7 475474.52

Using Integer, Float and Double objects with + operator

In above example, we used the primitive types for conversion. Similarly, you may use objects for converting into the string as using the + operator. This is shown in the demo below. There, I created three object variables:

The Output:

a String with number objects: 35  45.5 4547457.25

Using Java String.format() method for int to string conversion

You may also use String.format() method for the number conversions into string. Basically, String.format is bigger than that, but for our topic of int to string conversion, we may use this method as well.

See a demonstration below where not only int is converted but other data types as well by using format() method of String.

The output:

int –> String by format() = 1005

float –> String by format() = 105.500000

double –> String by format() = 1241214.524000

Display only 2 digits after deicimal point = 1241214.52

You can see, float and double with different precision also converted to strings.

Using StringBuffer and StringBuilder classes

Well, the usage of these classes for converting int or other types to strings can be useful if you are working with mutable strings. The StringBuffer and StringBuilder classes allow creating a string that can be modified, since; the String class’s strings are immutable in Java.

I will not go into further details of what are the StringBuffer or StringBuilder classes, as this is a separate topic but have a look at the examples below. First, using the StringBuffer class for converting int to string (along with float and double conversion). This is followed by an example of StringBuilder class.

With StringBuffer class:

The Output:

The final string:

Let’s add int = 10 and a float: 525.2 and finally double:1415414.14

Let’s add int = 10 and a float: 525.2 and finally double:1415414.14

Example with StringBuilder class

The difference between the StringBuilder and StringBuffer classes is that StringBuffers are safe for use by multiple threads while StringBuilder provides no guarantee of synchronization.

As for our topic for conversion into strings, if you simply change the object in above example from StringBuffer to StringBuilder, it will produce the same result. See the code and output below:

See online demo and code

The output:

int to string StringBuilder String-Buffer

I only changed the variable names and values.