Java

Converting int to string in Java

For an integer to be printed, it must be converted into a string. For an integer to become part of a string, it must also be converted into a string. This article explains how an integer can be converted into a string intentionally and by using some operations. None of these approaches need to import the string class into the program.

String.valueOf() Method

This static method takes an argument as int and returns a string form of the integer value. Here, static means that the string class name should be used without instantiating the string object. The full syntax for this method is:

public static String valueOf(int i)

The following program illustrates the use of this method for different integer values:

public class TheClass {
        public static void main(String[] args) {
            int i1 = 5, i2 = -5, i3 = 256, i4 = -256;

            String str1 = String.valueOf(i1); String str2 = String.valueOf(i2);
            String str3 = String.valueOf(i3); String str4 = String.valueOf(i4);

System.out.print(str1); System.out.print(", ");
System.out.print(str2); System.out.print(", ");
System.out.print(str3); System.out.print(", ");
System.out.print(str4); System.out.println();
        }
    }

The output is:

5, -5, 256, -256

The program begins with no import statement. All the code is in the main() method. The first line in the main method declares the integers with assignments. The second code segment does the conversions. The third code segment prints out the results.

Integer.toString()

There is a class with the name, Integer. It does not need to be imported by the program to be used. It is a wrapper to the primitive int type. It has a method, which is toString(). This method returns the string form of the integer value. The full syntax for this is:

public static String toString(int i)

It is a static method. Any static method does not require the instantiation of the class (String) in order to be used. The following program illustrates the use of this method for different integer values:

public class TheClass {
        public static void main(String[] args) {
            int i1 = 5, i2 = -5, i3 = 256, i4 = -256;

            String str1 = Integer.toString(i1); String str2 = Integer.toString(i2);
            String str3 = Integer.toString(i3); String str4 = Integer.toString(i4);

System.out.print(str1); System.out.print(", ");
System.out.print(str2); System.out.print(", ");
System.out.print(str3); System.out.print(", ");
System.out.print(str4); System.out.println();
        }
    }

The output is:

5, -5, 256, -256

The program begins with no import statement. All the code is in the main() method. The first line in the main method declares the integers with assignments. The second code segment does the conversions. The third code segment prints out the results.

StringBuffer or StringBuilder

The StringBuffer or StringBuilder class is like a string class. However, its characters can be changed, while the characters of the string class cannot be changed. Both of these classes have the append() method that can be used to add value to the object. The append method can take an int as an argument. The integer value is converted into a character and added to the string.

StringBuffer

The following program illustrates the case for a StringBuffer object:

    public class TheClass {
        public static void main(String[] args) {
            int i1 = 5, i2 = -5, i3 = 256, i4 = -256;

StringBuffer str1 = new StringBuffer(); StringBuffer str2 = new StringBuffer();
StringBuffer str3 = new StringBuffer(); StringBuffer str4 = new StringBuffer();
            str1.append(i1); str2.append(i2); str3.append(i3); str4.append(i4);

System.out.print(str1); System.out.print(", ");
System.out.print(str2); System.out.print(", ");
System.out.print(str3); System.out.print(", ");
System.out.print(str4); System.out.println();
        }
    }

The output is:

5, -5, 256, -256

The program begins with no import statement. All the code is in the main() method. The first line in the main method declares the integers with assignments. The second code segment does the conversions. The third code segment prints out the results.

StringBuilder

The following program illustrates the case for the StringBuilder object:

    public class TheClass {
        public static void main(String[] args) {
            int i1 = 5, i2 = -5, i3 = 256, i4 = -256;

            StringBuilder str1 = new StringBuilder(); StringBuilder str2 = new StringBuilder();
            StringBuilder str3 = new StringBuilder(); StringBuilder str4 = new StringBuilder();
            str1.append(i1); str2.append(i2); str3.append(i3); str4.append(i4);

System.out.print(str1); System.out.print(", ");
System.out.print(str2); System.out.print(", ");
System.out.print(str3); System.out.print(", ");
System.out.print(str4); System.out.println();
        }
    }

The output is:

5, -5, 256, -256

The program begins with no import statement. All the code is in the main() method. The first line in the main method declares the integers with assignments. The second code segment does the conversions. The third code segment prints out the results.

String.format()

The string class has the format() method. The full syntax is:

public static String format(String format, Object... args)

It is a static method. It consists of text interspersed with format specifiers. The first argument is called the format string, though it is still to be formatted. The second argument is an argument list. If the format string has only one specifier, the argument list should have only one argument, the integer value, not quotes. The specifier for integer is %d .

    public class TheClass {
        public static void main(String[] args) {
            int i1 = 5, i2 = -5, i3 = 256, i4 = -256;

            String str1 = String.format("%d", i1); String str2 = String.format("%d", i2);
            String str3 = String.format("%d", i3); String str4 = String.format("%d", i4);

System.out.print(str1); System.out.print(", ");
System.out.print(str2); System.out.print(", ");
System.out.print(str3); System.out.print(", ");
System.out.print(str4); System.out.println();
        }
    }

The output is:

5, -5, 256, -256

The program begins with no import statement. All the code is in the main() method. The first line in the main method declares the integers with assignments. The second code segment does the conversions. The third code segment prints out the results.

String Concatenation Operator

The string concatenation operator is + . If an empty string is concatenated with an integer, that integer becomes the string. The following program illustrates this:

    public class TheClass {
        public static void main(String[] args) {
            int i1 = 5, i2 = -5, i3 = 256, i4 = -256;

            String str1 = "" + i1; String str2 = "" + i2;
            String str3 = "" + i3; String str4 = "" + i4;

System.out.print(str1); System.out.print(", ");
System.out.print(str2); System.out.print(", ");
System.out.print(str3); System.out.print(", ");
System.out.print(str4); System.out.println();
        }
    }

The output is:

5, -5, 256, -256

The program begins with no import statement. All the code is in the main() method. The first line in the main method declares the integers with assignments. The second code segment does the conversions. The third code segment prints out the results.

Conclusion

The following methods and operator, can be used to convert an integer into a string: String.valueOf(), Integer.toString(), StringBuffer.append(), StringBuilder.append(), String.format() and the string concatenation operator.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.