Java Convert int to String – Different Ways of Conversion
Last Updated :
09 Apr, 2025
Converting an int to a String is an important type conversion. Many operations can be performed over a string, while we are limited when it comes to integers. We have a wide varied list of in-built methods in the String class that help us perform hassle-free operations.
Suppose we are required to concatenate two integers then it would become a tedious for us. We need to go through as we need to deal with the number system, corresponding to which we will be playing mathematics within the number system. But to convert integers to strings in Java, we have some inbuilt methods and classes which make our work too easy.
Tip: We generally convert primitive class data members types though we have the concept of Wrapper classes to Strings because in practical programming in java we deal with strings.
Ways to Convert int to a String in Java
Converting Integers to Strings involves using the Integer classes toString() or String.valueOf() for direct conversion. String.format() is another method offering flexible formatting options. Using StringBuilder or StringBuffer for appending integer values as strings is efficient for extensive string manipulation.
There are certain methods for Integer to String conversions mentioned below:
- Using the toString() method of the Integer class
- Using the valueOf() method of the String class
- Using Integer(int).toString() method of Integer class
- Using concatenation with an empty string.
1. Using toString() Method of Integer Class
The Integer class has a static method toString() that returns a String object representing the specified int parameter. The argument is converted and returned as a string instance. If the number is negative, the sign will be preserved.
Example:
Java
// Java Program to Illustrate Integer to String Conversions
// Using toString() Method of Integer Class
class Geeks
{
// Main driver method
public static void main(String args[])
{
// Custom input integers
int a = 1234;
int b = -1234;
// Converting integer to string
// using toString() method
String str1 = Integer.toString(a);
String str2 = Integer.toString(b);
// Printing the above strings that
// holds integer
System.out.println("String str1 = " + str1);
System.out.println("String str2 = " + str2);
}
}
OutputString str1 = 1234
String str2 = -1234
2. Using valueOf() Method of String Class
The String class has a static method valueOf() that can be used to convert the Integer to String as shown below:
Example:
Java
// Java Program to Illustrate Integer to String Conversions
// Using valueOf() Method of String class
class Geeks
{
// Main driver method
public static void main(String args[])
{
// Custom integer input
int c = 1234;
// Converting above integer to string
// using valueOf() Method
String str3 = String.valueOf(c);
// Printing the integer stored in above string
System.out.println("String str3 = " + str3);
}
}
3. Using Integer(int).toString() Method of the Integer Class
It is different from method 1 as proposed above, as in this method we use an instance of the Integer class to invoke its toString() method.
Example:
Java
// Java Program to Illustrate
// Integer to String Conversions
// Using toString() Method of
// Integer Class
// Importing required classes
import java.util.*;
// Main class
class Geeks
{
// Main driver method
public static void main(String args[])
{
// Custom input integer
int d = 1234;
// Converting integer to string
// using toString() method of Integer class
String str4 = new Integer(d).toString();
// Printing the integer value stored in above string
System.out.println("String str4 = " + str4);
}
}
Explanation: If the variable is of primitive type (int), it is better to use Integer.toString(int) or String.valueOf(int). But if the variable is already an instance of Integer (wrapper class of the primitive type int), it is better to just invoke its toString() method as shown above.
Note: This method is not efficient as an instance of the Integer class is created before conversion is performed. And deprecated and marked as removal.
4. Using Concatenation with an Empty String
Here, we will declare an empty string and using the ‘+’ operator, we will simply store the resultant as a string. Now by this, we are successfully able to append and concatenate these strings.
Example:
Java
// Java Program to Illustrate Integer to String Conversions
// Using Concatenation with Empty String
class Geeks
{
// Main driver method
public static void main(String args[])
{
// Custom integer values
int a = 1234;
int b = -1234;
// Concatenating with empty strings
String str1 = "" + a;
String str2 = "" + b;
// Printing the concatenated strings
System.out.println("String str1 = " + str1);
System.out.println("String str2 = " + str2);
}
}
OutputString str1 = 1234
String str2 = -1234
Advanced Methods to Convert int to String
There are certain advance Methods are mentioned below:
- Using DecimalFormat Class
- Using StringBuffer class
- using StringBuilder class
- Using special radix and custom radix
1. Using DecimalFormat Class
DecimalFormat is a class that formats a number to a String.
Example:
Java
// Java Program to Illustrate
// Integer to String Conversions
// Using DecimalFormat Class
// Importing required classes
import java.text.DecimalFormat;
// Main class
class Geeks
{
// Main driver method
public static void main(String args[])
{
// Input integer value
int e = 12345;
// Creating an object of DecimalFormat class
// inside main() method
DecimalFormat df = new DecimalFormat("#,###");
// Converting above integral value to string
String Str5 = df.format(e);
// Printing the value stored in above string
System.out.println(Str5);
}
}
Tip: Using this method, We can specify the number of decimal places and comma separator for readability.
2. Using StringBuffer Class
StringBuffer is a class that is used to concatenate multiple values into a String.
Example 1:
Java
// Java Program to Illustrate
// Integer to String Conversions
// Using StringBuffer Class
// Main class
class Geeks {
// Main driver method
public static void main(String args[])
{
// Integer input value
int f = 1234;
// Creating an object of StringBuffer class
StringBuffer sb = new StringBuffer();
sb.append(f);
String str6 = sb.toString();
System.out.println("String str6 = " + str6);
}
}
Example 2:
Java
// Java Program to Illustrate
// Integer to String Conversions
// Using StringBuffer Class
class Geeks
{
// Main driver method
public static void main(String args[])
{
String str6
= new StringBuffer().append(1234).toString();
System.out.println("String str6 = " + str6);
}
}
3. Using StringBuilder Class
StringBuilder works similarly but is not thread-safe like StringBuffer.
Example 1:
Java
// Java Program to Illustrate
// Integer to String Conversions
// Using StringBuilder Class
// Main class
class Geeks
{
// Main driver method
public static void main(String args[])
{
// Input integer
int g = 1234;
// Creating an object of StringBuilder class
// inside main() method
StringBuilder sb = new StringBuilder();
sb.append(g);
String str7 = sb.toString();
// Printing the value stored in above string
System.out.println("String str7 = " + str7);
}
}
Example 2:
Java
// Java Program to Illustrate Different Ways for
// Integer to String Conversions
// Using StringBuilder Class
// Main class
class Geeks
{
// Main driver method
public static void main(String args[])
{
String str7
= new StringBuilder().append(1234).toString();
// Printing the value stored in above string
System.out.println("String str7 = " + str7);
}
}
Note: All the examples above use the base (radix) 10. Following are convenient methods to convert to binary, octal, and hexadecimal systems. The arbitrary custom number system is also supported.
Converting int to String With Different Base
We can also convert int to String in different bases below are the some of the important methods are mentioned which help to convert int to String with different base.
1. Using Special Radix
Example 1: Converting int(base 2 or binary number) to String using Integer class method toBinary().
Java
// Java Program to Illustrate Integer to String Conversions
// Using Special Radix In Binary Numbers
// Main class
class Geeks
{
// Main driver method
public static void main(String args[])
{
// Input integer
int h = 255;
String binaryString = Integer.toBinaryString(h);
// Printing the binary number stored in above string
System.out.println(binaryString);
}
}
Explanation: 11111111 is the binary representation of the number 255.
Example 2: Converting int(base 8 or octal number) to String using Integer class method toOctalString().
Java
// Java Program to Illustrate Integer to String Conversions
// Using Special Radix In Octal Numbers
// Main class
class Geeks
{
// Main driver method
public static void main(String args[])
{
// Custom input integer
int i = 255;
String octalString = Integer.toOctalString(i);
// Printing the octal number stored in above string
System.out.println(octalString);
}
}
Explanation: 377 is the octal representation of the number 255.
Example 3: Converting int(base 10 or Hexadecimal number) to String using Integer class method toHexString().
Java
// Java Program to Illustrate Integer to String Conversions
// Using Special Radix In Hexadecimal Numbers
// Main class
class Geeks
{
// Main driver method
public static void main(String args[])
{
// Custom input integer
int j = 255;
String hexString = Integer.toHexString(j);
// Printing the hexadecimal number
// stored in above string
System.out.println(hexString);
}
}
Explanation: The ff is the hexadecimal representation of the number 255.
2. Custom Base/Radix
Approach: We are using the toString() method of the Integer class to get it converted into a string where additionally we will be passing a value as an argument known as radix. One can use any other custom base/radix when converting an int to a string. In the example below, we are considering the base 7 number system for illustration purposes.
Example: Converting int(base 7 or custom radix number) to String using Integer class method toString(int I, int radix).
Java
// Java Program to Illustrate Integer to String Conversions
// Using Custom Radix
// Main class
class Geeks {
// Main driver method
public static void main(String args[])
{
// Input integer value
int k = 255;
// Setting base as 7, converting integer to string
// using toString() method and
// storing it into a string
String customString = Integer.toString(k, 7);
// Printing value stored in above string
System.out.println(customString);
}
}
Note: 513 is the representation of the number 255 when written in the base 7 system.
Similar Reads
Different ways for String to Integer Conversions in Java
Given a String in Java, the task is to convert this String into Integer. Examples: Input: str = "1234" Output: 1234 Input: str = "456" Output: 456 Convert using Integer.parseInt(String) The Integer class has a static method that returns an integer object representing the specified String parameter.
2 min read
How to convert a String to an Int in Java?
In Java, converting a String to an int is done using methods from the Integer class. The methods used for this conversion are Integer.parseInt() and Integer.valueOf(). Example: The Integer.parseInt() is a commonly used method to convert a string to an integer in Java. This method converts a numeric
1 min read
Convert List of Characters to String in Java
Given a list of characters. In this article, we will write a Java program to convert the given list to a string. Example of List-to-String ConversionInput : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc" Strings - Strings in Java are objects that are
4 min read
Convert String to Double in Java
In this article, we will convert a String to a Double in Java. There are three methods for this conversion from String to Double, as mentioned below in the article. Methods for String-to-Double ConversionDifferent ways for converting a String to a Double are mentioned below: Using the parseDouble()
3 min read
Program to convert String to IntStream in Java
Given a String, the task is to convert this String into an IntStream containing the ASCII values of the characters as the elements. Examples: Input: String = Geeks Output: 71, 101, 101, 107, 115 Input: String = GeeksForGeeks Output: 71, 101, 101, 107, 115, 70, 111, 114, 71, 101, 101, 107, 115 Algori
1 min read
Program to convert IntStream to String in Java
Given a Instream containing ASCII values, the task is to convert this Instream into a String containing the characters corresponding to the ASCII values. Examples: Input: IntStream = 71, 101, 101, 107, 115 Output: Geeks Input: IntStream = 71, 101, 101, 107, 115, 70, 111, 114, 71, 101, 101, 107, 115
1 min read
5 Ways to Convert Double to Integer in Java
Double is a primitive data type in Java that is used to represent double-precision floating point numbers. It uses the IEEE 754 standard for representing floating point numbers and has a default value of 0.0d. The double data type is the default data type for decimal numbers in Java and has a defaul
5 min read
Ways to Read Input from Console in Java
In Java, there are four different ways to read input from the user in the command line environment(console). 1. Using Buffered Reader ClassBuffered Reader Class is the classical method to take input, Introduced in JDK 1.0. This method is used by wrapping the System.in (standard input stream) in an I
5 min read
Different Ways to Generate String by using Characters and Numbers in Java
Given a number num and String str, the task is to generate the new String by extracting the character from the string by using the index value of numbers. Examples: Input: str = âGeeksforGeeksâ num = 858 Output: GfG Explanation: The 8th, 5th, and 8th position of the characters are extracting from th
3 min read
Different Ways to Remove all the Digits from String in Java
Given alphanumeric string str, the task is to write a Java program to remove all the digit from this string and prints the modified string. Examples: Input: str = âGeeksForGeeks123âOutput: GeeksForGeeksExplanation: The given string contains digits 1, 2, and 3. We remove all the digit and prints the
3 min read