Java int to string Conversion

Informative infographic showcasing key techniques for converting integers to strings in Java. Explore String.valueOf, Integer.toString, and concatenation methods.

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.

The following methods can be used for conversion:

Preferred ways

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

Other ways of converting numbers to string:

  • format()
  • StringBuffer
  • StringBuilder

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:

public class conversion_demo {

          public static void main(String args[]) {
           int x = 50;
           Integer y = 10;

           //converting numbers
           String str_1 = Integer.toString(x);
           String str_2 = y.toString();

           System.out.println("After int to string = " + str_1);
           System.out.println("After integer to string = " + str_2);

     }

  }

The output of above code is:

After int to string = 50
After integer to string = 10

You saw that an int and the other Integer type variables were 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:

public class conversion_demo {

          public static void main(String args[]) {

           float x = 50.55f;
           Float y = 66.45f;     

           //converting numbers
           String str_f1 = Float.toString(x);
           String str_f2 = y.toString();
       
           System.out.println("After float to string = " + str_f1);
           System.out.println("After Float to string = " + str_f2);

        }

  }

The output:

After float to string = 50.55

After Float to string = 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:

public class conversion_demo {

          public static void main(String args[]) {
           double x = 50.55d;
           Double y = new Double(15.5);
         

           //converting numbers
           String s_d1 = Double.toString(x);
           String s_d2 = y.toString();

           System.out.println("After double to string = " + s_d1);
           System.out.println("After Double to string = " + s_d2);

        }

     }

The output of above example is:

After double to string = 50.55

After Double to string = 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:

public class conversion_demo {

          public static void main(String args[]) {
                 int a = 10;
                 Integer obj_b = new Integer(20);

                 float c = 9.9f;

           Float obj_d = new Float(10.9f);               

                 long e = 124574;
                 Long obj_f = new Long(12457454);               
                 double g = 9.9d;

           Double obj_h = new Double(1024547.9d);               
          

           System.out.println("int to string = " + String.valueOf(a));
           System.out.println("Integer Object to string = " + String.valueOf(obj_b) +"\n");
           System.out.println("float to string = " + String.valueOf(c));
           System.out.println("Float Object to string = " + String.valueOf(obj_d)+"\n");
           System.out.println("long to string = " + String.valueOf(e));
           System.out.println("Long object to string = " + String.valueOf(obj_f)+"\n");
           System.out.println("double to string = " + String.valueOf(g));
           System.out.println("Double object to string = " + String.valueOf(obj_h));
        }
    }

The output:

int to string = 10Integer Object to string = 20
float to string = 9.9Float Object to string = 10.9
long to string = 124574Long object to string = 12457454
double to string = 9.9
Double object to string = 1024547.9

An example of a concatenation operator (+) for conversion

The plus sign (+) is a string concatenation operator that is also used for the 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:

public class conversion_demo {

          public static void main(String args[]) {

                 int int_a = 77;

                 float flt_b = 24.7f;

                 double dbl_c = 475474.52d;

                //Creating string with + operator

                 String str = "a String " + int_a +" "  +" " + flt_b +" " + dbl_c;
                 System.out.println(str);
        }

    }

The Output:

a String: 77  24.7 475474.52

Using Integer, Float, and Double objects with ‘+’ operator

In the 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:

public class conversion_demo {

          public static void main(String args[]) {

                 Integer obj_int = new Integer(35);

                 Float obj_flt = new Float(45.5f);

                 Double obj_dbl = new Double(4547457.25d);

                 //Creating string with + operator
                 String str = "a String with number objects: " + obj_int +" "  +" " + obj_flt +" " + obj_dbl;

                 System.out.println(str);

      }

  }

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.

public class conversion_demo {

          public static void main(String args[]) {

                 int x = 1005;
                 float y = 105.5f;
                 double z = 1241214.524d;

                 String str_int = String.format ("%d", x);
                 String str_flt = String.format ("%f", y);
                 String str_dbl = String.format ("%f", z);
                 String str_dbl2 = String.format ("%.2f", z);


                 System.out.println("int --> String by format() = " + str_int);
                 System.out.println("float --> String by format() = " + str_flt);
                 System.out.println("double --> String by format() = " + str_dbl);
                 System.out.println("Display only 2 digits after decimal point = " + str_dbl2);
        }

    }

The output:

int –> String by format() = 1005

float –> String by format() = 105.500000

double –> String by format() = 1241214.524000

Display only 2 digits after decimal 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 to creation of 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:

public class conversion_demo {

          public static void main(String args[]) {

                 int i = 10;
                 float f = 525.2f;
                 double d = 1415414.14d;

                 StringBuffer sb_conv = new StringBuffer();

                 //Appending in StringBuffer object - strings, int, float and numbers
                 sb_conv.append("Let's add int = ");
                 sb_conv.append(i);
                 sb_conv.append(" and a float: ");
                 sb_conv.append(f);
                 sb_conv.append(" and finally double:");
                 sb_conv.append(d);

                 //Using toString() method of StringBuffer for creating a string now
                 String strI = sb_conv.toString();

                 System.out.println("The final string: \n" + strI);

                 //You may directly use the mutable StringBuffer object as well
                 System.out.println(sb_conv);

    }

 }

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

If you simply change the object in the above example from StringBuffer to StringBuilder, it will produce the same result.

See the code and output below:

public class conversion_demo {

          public static void main(String args[]) {

                 int a = 105;
                 float b = 25.7f;
                 double c = 1424541.28d;

                 StringBuilder sb_conv_demo = new StringBuilder();

                 //Appending in StringBuffer object - strings, int, float and numbers
                 sb_conv_demo.append("Let's add int = ");
                 sb_conv_demo.append(a);
                 sb_conv_demo.append(" and a float: ");
                 sb_conv_demo.append(b);
                 sb_conv_demo.append(" and finally double:");
                 sb_conv_demo.append(c);


                 //Using toString() method of StringBuffer for creating a string now
                 String strI = sb_conv_demo.toString();

                 System.out.println("The final string: \n" + strI);
                 //You may directly use the mutable StringBuffer object as well
                 System.out.println(sb_conv_demo);
        }
   }

The output:

int to string StringBuilder String-Buffer

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.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!