Java- Convert Int to String

In Java programming, we’ll learn to Convert Int to String in Java. This essential transformation is often required in various scenarios, including handling user input, formatting data for display, and concatenating strings with numerical values. In this blog, you’ll learn how to convert integers to strings in Java in the following ways:

Using to String() method of Integer class

// Convert Int to String in Java
class IntStringEx {    
    public static void main(String args[]){
        // Custom input integers
        int x = 812;
        int y = -542;
 
        // Converting integer to string using toString() method
        String string1 = Integer.toString(x);
        String string2 = Integer.toString(y);
 
        // Printing the above strings that holds integer
        System.out.println("String1 = " + string1);
        System.out.println("String2 = " + string2);
    }
}

Output:

String1 = 812
String2 = -542

Explanation of code:

The given Java code explains How to Convert int into string in Java using the `Integer.toString()` method. Two custom integers, `x` and `y`, are declared. The `toString()` method is then applied to convert each integer into a string (`string1` and `string2`). Finally, the converted strings are printed. This method provides a straightforward way to transform integer values into their string representations. It is particularly useful when you need to concatenate integers with other strings, display numeric values, or work with data that requires a string format. The code emphasizes simplicity and clarity in handling such conversions in Java.

Using valueOf() method of String class

// Convert Int to String in Java
class IntStringEx {    
    public static void main(String args[]){
        // Custom integer input
        int m = 184;
 
        // Converting above integer to string using valueOf() Method
        String string = String.valueOf(m);
 
        // Printing the integer stored in above string
        System.out.println("String = " + string);
    }
}

Output:

String = 184

Explanation of code:

The given Java code explains How to Convert int into string in Java using the `String.valueOf()` method. In this example code, the custom integer value, `184`, is transformed into a string named `string`. The `System.out.println()` statement then outputs the converted string. This approach is straightforward and commonly used in Java for converting integers to strings, offering a quick and efficient way to handle scenarios where numerical values need to be represented or manipulated as strings in a program.

Using Integer(int).toString() method of Integer class

// Convert Int to String in Java
import java.util.*;
public class IntStringEx {
    public static void main(String args[]){
        // Custom input integer
        int n = 134;
        Integer obj = new Integer(n);
        String str = obj.toString();
 
        // Printing the above string holding integer value
        System.out.println("String str = " + str);
    }
}

Output:

String str = 134

Explanation of code:

The given Java code explains How to Convert int into string in Java using the `toString()` method of Integer class. In this example, a custom integer value, `134`, is first wrapped into an `Integer` object (`obj`). The `toString()` method is then applied to this object, transforming it into a string named `str`. The `System.out.println()` statement is employed to display the resulting string. This approach, utilizing the `toString()` method, showcases an alternative method for converting integers to strings in Java, providing flexibility in different coding scenarios.

Using DecimalFormat Class

// Convert Int to String in Java
import java.text.DecimalFormat;
public class IntStringEx {
    public static void main(String args[])
    {
        int e = 532;
        DecimalFormat df = new DecimalFormat("#");
 
        // Formatting the integer to string and storing it in a string
        String str = df.format(e);
 
        // Printing the above stored value inside a string
        System.out.println(str);
    }
}

Output:

532

Explanation of code:

The given Java code explains ‘How to Convert int into string’ in Java using the `DecimalFormat` class. Beginning with the integer value `532`, the code utilizes `DecimalFormat` with the pattern `#` to format the integer as a string. The resulting formatted string is stored in the variable `str`. The `System.out.println()` statement is then employed to display this formatted string. By leveraging `DecimalFormat`, the code allows for tailored formatting of integers to strings, offering control over how numerical values are presented in Java programs.

Explore the fascinating world of OOPs in Java Check out!

Using StringBuffer class

// Convert Int to String in Java
class GFG {
    public static void main(String args[])
    {
        int f = 145;
 
        // Creating an object of StringBuffer class
        StringBuffer sb = new StringBuffer();
        sb.append(f);

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

Output:

String str = 145

Explanation of code:

The given Java code explores concept of how to  ‘Convert int into string using the `StringBuffer` class. The code initializes an integer value, `145`, and appends it to a `StringBuffer` object (`sb`) using the `append()` method. The content of the `StringBuffer` is then converted to a string, stored in the variable `str` using the `toString()` method. Finally, the resulting string is printed using `System.out.println()`. Utilizing `StringBuffer` for int to String conversion allows for mutable string manipulation, providing a dynamic approach for handling and modifying string representations in Java programs.

Dive into our interview question bank and equip yourself with the knowledge needed to excel in Java interviews!

Using StringBuilder class

// Convert Int to String in Java
class IntStringEx {
    public static void main(String args[])
    {
        // Input integer
        int g = 654;

        StringBuilder sb = new StringBuilder();
        sb.append(g);
 
        String str = sb.toString();
 
        // Printing the value stored in above string
        System.out.println("String str = " + str);
    }
}

Output:

String str = 654

Explanation of code:

The Java code demonstrates how to ‘convert int to string using the `StringBuilder` class’. Initially, a custom integer value, `654`, is appended to a `StringBuilder` object (`sb`) using the `append()` method. The content of the `StringBuilder` is then transformed into a string named `str` with the `toString()` method. Finally, the `System.out.println()` statement is employed to showcase the resulting string. Leveraging `StringBuilder` for int to String conversion in Java allows for efficient and mutable string manipulation, providing a versatile approach for handling numerical values in string format.

Using concatenation with an empty string

// Convert Int to String in Java
class IntStringEx {
    public static void main(String args[]){
        // Custom integer values
        int a = 1245;
        int b = -1474;
 
        // Concatenating with empty strings
        String str1 = "" + a;
        String str2 = "" + b;
 
        // Printing the concatenated strings
        System.out.println("String str1 = " + str1 +"\n");
        System.out.println("String str2 = " + str2);
    }
}

Learn How to Convert String to Int in Java, Here!

Output:

String str1 = 1245

String str2 = -1474

Explanation of code:

The provided Java code showcases an alternative method how to convert  int to strings using concatenation with empty strings. The custom integer values, `1245` and `-1474`, are concatenated with empty strings, resulting in two strings named `str1` and `str2`. The `System.out.println()` statements are then used to print the concatenated strings. This approach leverages the automatic conversion feature in Java, where adding a string to any data type results in a string representation, making it a concise and straightforward method for int to String conversion in certain scenarios.

In conclusion, converting int to String in Java offers versatile methods, each catering to specific needs. From Integer class’s toString() and valueOf() to DecimalFormat, StringBuffer, StringBuilder, and simple concatenation, developers have an array of tools. Choose the method that aligns with your preferences and project requirements for seamless int-to-String conversion.

We hope our blog on ‘Convert Int to String in Java’ was helpful and provided useful insight into conversion Programs in Java. Make sure to visit our Newtum website for additional information on a variety of subjects, including PHP, C Programming for kids, Java, and more. Happy coding!

About The Author