Methods to Convert Integer to String in Java

Methods to Convert Integer to String in Java

3 mins read130 Views Comment
Updated on Feb 28, 2023 20:30 IST

In this article, we will learn different methods to convert an integer to a string with the help of examples.

2023_02_MicrosoftTeams-image-198.jpg

In this article, we will learn how to convert an integer to a string in Java, where integers and strings are different data types in Java. This article will briefly discuss different methods to convert integers to strings.
So, before moving further let’s discuss what is int data types in java?

int is a primitive data type in java used to declare a variable in the program. Some points to remember about int are:

  • The default value of int is 0
  • The default size of int is 4 bytes
  • The range of int is from – 231 to 231

int is declared in Java as:

 
int a = 100;
Copy code

Note: Don’t be confused with int and Integer in Java since, int is a non-primitive data type in java, whereas Integer is wrapper class in Java.

Why do we need to convert int to String?

In the programming world, we come across various scenarios where we have to perform operations which we do easily with String as compared to an integer because of various inbuilt methods present in String. When it comes to integers, inbuilt methods are a little.

Suppose we want to concatenate two integers, then it is a complex task, then do the same thing with String. We have to dig into the number system, do mathematics, and make functions to accomplish the concatenation of integers. Still, with String, we have to use the concate() method or the “+” operator for the same task. So, that’s why we convert integers into String to accomplish such problems that we face while writing programs or algorithms.

Must Read: What is Java?

Must Check: Java Online Courses and Certification

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
6 weeks
15 K
3 months
2.05 K
3 months
– / –
170 hours
5.4 K
1 month
– / –
6 months
– / –
2 months
17 K
3 months
5.5 K
1 month

Methods to Convert an Integer to a String

By using Integer.toString()

Let’s see the below program in which we convert an integer into a String by using Integer.toString() method.

Program:

 
public class Main{
public static void main(String args[]) {
// create an integer variable a and assign 100 value to it
int a=100;
//Convert integer a to String by using Integer.toString() method
String hundred=Integer.toString(a);
//Print String variable hundred
System.out.println(hundred);
}
}
Copy code

Output:

2023_02_image-181.jpg

By using String.valueOf()

Let’s see the below program in which we convert an integer into a String by using String.valueOf() method.

Program:

 
public class Main{
public static void main(String args[]) {
// create an integer variable a and assign 100 value to it
int a=100;
//Convert integer a to String by using String.valueOf() method
String hundred=String.valueOf(a);
//Print String variable hundred
System.out.println(hundred);
}
}
Copy code

Output:

2023_02_image-182.jpg

By using String.format()

Let’s see the below program in which we convert an integer into a String by using String.format() method.

Program:

 
public class Main{
public static void main(String args[]) {
// create a integer variable a and assign 100 value to it
int a=100;
//Convert integer a to String by using String.format() method
String hundred=String.format("%d",a);
//Print String variable hundred
System.out.println(hundred);
}
}
Copy code

Output:

2023_02_image-183.jpg

By using Concatenation with Empty String

In the below program, we create an empty String and merge the integer with the “+” operator and store the resultant as String.

Program:

 
public class Main{
public static void main(String args[]) {
// create a integer variable a and assign 10 value to it
int a=10;
//Creating an empty String
String empty_string="";
//merge integer with empty String
String ten=empty_string+10;
//printing String ten
System.out.println(ten);
}
}
Copy code

Output

2023_02_image-189.jpg

By using the StringBuffer class

In the below program, we first add an integer value to the StringBuffer object and then convert that value into a String using the toSrting() method of the StringBuffer class.

Program:

 
public class Main{
public static void main(String args[]) {
// create a integer variable a and assign 10 value to it
int a=10;
//Creating object of StringBuffer class
StringBuffer sb=new StringBuffer();
// append() function add or append the value to StringBuffer object
sb.append(a);
//value contained inside StringBuffer is convert into String
String ten=sb.toString();
//print String ten
System.out.println(ten);
}
}
Copy code

Output:

2023_02_image-185.jpg

By using the StringBuilder class

In the below program, we first add an integer value to the StringBuilder object and then convert that value into a String using the toSrting() method of the StringBuilder class.

Program:

 
public class Main{
public static void main(String args[]) {
// create a integer variable a and assign 10 value to it
int a=10;
//Creating object of StringBuilder class
StringBuilder sb=new StringBuilder();
// append() function add or append the value to StringBuilder object
sb.append(a);
//value contained inside StringBuilder is convert into String
String ten=sb.toString();
// printing String ten
System.out.println(ten);
}
}
Copy code

Output:

2023_02_image-186.jpg

By using DecimalFormat class

In the below program, we use the DecimalFormat class and call format() method to convert integer into String.

Program:

 
//Import required class
import java.text.DecimalFormat;
public class Main{
public static void main(String args[]) {
// create a integer variable a and assign 10 value to it
int a=10;
//creating object of DecimalFormat class
DecimalFormat df=new DecimalFormat();
//converting integer a into String using format method and store it in String ten
String ten=df.format(a);
//printing String ten
System.out.println(ten);
}
}
Copy code

Output

2023_02_image-187.jpg

By using the to String() method of the Integer class

In the below program, we create an Integer class object and call the toString() method to convert the integer into a String.

Program:

 
public class Main{
public static void main(String args[]) {
// create a integer variable a and assign 10 value to it
int a=10;
//creating object of Integer
Integer obj=new Integer(a);
//converting integer into String using toString() method of Integer class
String ten=obj.toString();
//printing String ten
System.out.println(ten);
}
}
Copy code

Output:

2023_02_image-188.jpg

Conclusion

In this article, we have discussed different methods to convert an integer to a string with the help of examples.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

Contributed By: Shubham Kumar

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio