How To Convert Int To String In Java

Convert Int To String In Java | In this blog, we will see how to convert int to string in the Java programming language. Along with this, we will also see examples of converting an Integer object to a String type. In order to do this Java has some in-built methods that make the tasks easy.

We can convert int to string in Java by the following methods-
1) By toString() method of Integer class.
2) By using String.valueOf() method.
3) By using DecimalFormat class.
4) By using the StringBuilder class.
5) By using the StringBuffer class.

Java Convert Int To String Using Integer.toString()

Like every other wrapper class, the Integer class also contains the toString() method and it can convert integer or int to string type. The method signature of the Integer.toString() method is as follows:- public static String toString(int i)

public class Main {

   public static void main(String args[]) {
      int num1 = 5648;
      int num2 = -12356;
      Integer num3 = 1289;

      String string1 = Integer.toString(num1);
      String string2 = Integer.toString(num2);
      String string3 = Integer.toString(num3);

      System.out.println("String-1 = " + string1);
      System.out.println("String-2 = " + string2);
      System.out.println("String-3 = " + string3);
   }
}

Output:-

String-1 = 5648
String-2 = -12356
String-3 = 1289

Convert Int To String In Java Using String.valueOf()

Now let us see how to convert int to string in Java using the String.valueOf() method. String class contains overloaded methods of valueOf() to convert primitive and object to string type. The valueOf() method signature to convert from int to string is as follows:- public static String valueOf(int i) and to convert Integer object to string it has:- public static String valueOf(Object obj)

public class Main {
   public static void main(String args[]) {
      int num1 = 5648;
      Integer num2 = 1289;

      String string1 = String.valueOf(num1);
      String string2 = String.valueOf(num2);

      System.out.println("String-1 = " + string1);
      System.out.println("String-2 = " + string2);
   }
}

Output:-

String-1 = 5648
String-2 = 1289

Java Convert Int To String using DecimalFormat.format()

DecimalFormat contains multiple overloaded forms of the format() method. The method signature used to convert int to string is:- public final String format(long number) and the method signature used to convert Integer to string is:- public final String format (Object obj). Since format() is not a static method therefore before calling the format() method we have to create an object of the DecimalFormat class.

import java.text.DecimalFormat;

public class Main {
   public static void main(String args[]) {
      int num1 = 12345;
      Integer num2 = 4597;

      DecimalFormat decimalFormat = new DecimalFormat("#");

      String string1 = decimalFormat.format(num1);
      String string2 = decimalFormat.format(num2);

      System.out.println("String-1 = " + string1);
      System.out.println("String-2 = " + string2);
   }
}

Output:-

String-1 = 12345
String-2 = 4597

Java Convert Int To String using StringBuilder

We can create a StringBuilder object and add an int or integer value in StringBuilder. After that, we can convert StringBuilder to string type. Using this way we can convert int to string using StringBuilder with the help of append() and toString() of the StringBuilder class.

public class Main {

   public static void main(String args[]) {
      int num = 48796;
      // Integer num = 48796;

      StringBuilder sb = new StringBuilder();
      sb.append(num);

      String string = sb.toString();
      System.out.println("String= " + string);
   }
}

Output:-

String = 48796

How To Convert Int To String In Java using StringBuffer

Similar to the StringBuilder class we can also take the help of the StringBuffer class. Here also, we need to create an object of the StringBuffer class and then call the append() method to add an int or integer to it. After that call toString() method on the StringBuffer object to convert StringBuffer to string. Finally, we will get the string representation of the int or integer type.

public class Main {

   public static void main(String args[]) {
      int num = 562317;
      // Integer num = 562317;

      StringBuffer sb = new StringBuffer();
      sb.append(num);

      String string = sb.toString();
      System.out.println("String= " + string);
   }
}

Output:-

String = 562317

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *