Convert int to string Java Example (with video) (original) (raw)

Integer to String conversion is a basic task in many Java projects. There are many ways to convert int to string in java, where some of them are very simple.

In this example, we will show all the possible ways via an example on Java convert int to string.

You can also check this tutorial in the following video:

String to int and int to String in Java – Video

1. Syntax of Integer.toString() method

There are two different expressions for Integer.toString() method:

The parameters of this method are:

The radix value is optional and if it is not set, the default value is 10, for the decimal base system.

java int to string

The returned value for both expressions is a String that represents the i argument. If radix parameter is used, the returned string is specified by the respective radix.

2. Int to String with String.valueOf() method

String.valueOf() is another static utility method. It is expressed:

where the parameter i is the converted int.

This method returns the string that is represented of the int argument.

3. Int to String with String.format() method

Another use case for Integer to String conversion is format() method. There are two different expressions:

The arguments for this method are:

The returned value of this method is a formatted string, specified by the arguments. It is important to mention that this is a new method because it is introduced in JDK 1.5.

4. StringBuilder Class

A more complex way for converting int to string is the use of StringBuilder class. StringBuilder object represent String object that can be modified and is treated as an array with a sequence of characters. To add a new argument to the end of the string, StringBuilder instance implements append() method. In the end it is important to call toString() method, in order to take the string representation of the data in this sequence.

5. Int to string Java conversion

Create a java class with the name IntToStringTest.java and paste the following code.

IntToStringTest.java:

0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647 package com.javacodegeeks.javabasics.inttostring;import java.util.IllegalFormatException;public class IntToStringTest { public static void main(String args[]) { int mainInt = 123456789; String string1 = "" + mainInt; System.out.println("With + operator: string1 = " + string1); String string2 = "123"; System.out.println("Directly in the String: string2 = " + string2); String string3 = Integer.toString(mainInt); String string4 = Integer.toString(mainInt, 16); System.out.println("With toString method: string3(10 base system) = " + string3 + ", string4(16 base system) = " +string4); String string5 = String.valueOf(mainInt); System.out.println("With valueOf method: string5 = " + string5); try{ String string6 = String.format("%d", mainInt); System.out.println("With format method: string6 = " + string6); } catch(IllegalFormatException e1) { System.err.println("IllegalFormatException: "+ e1.getMessage()); } catch(NullPointerException e2) { System.err.println("NullPointerException: "+ e2.getMessage()); } StringBuilder sb = new StringBuilder(); sb.append(mainInt); String string7 = sb.toString(); System.out.println("With StringBuilder class: string7 = " + string7); }}

As you can see in the code above, the most easy and simple way of int to string conversion is the + operator and/or the direct definition of the int into the string declaration. string1 and string2 show these situations. Notice that we defined %d as format specifier into the String.format(), in order to specify the integer. Also, this method throws IllegalFormatException to catch illegal syntax of the format string and NullPointerException if the format argument is null.

Below you can see the output of the execution.

Output:

With + operator: string1 = 123456789 Directly in the String: string2 = 123 With toString method: string3(10 base system) = 123456789, string4(16 base system) = 75bcd15 With valueOf method: string5 = 123456789 With format method: string6 = 123456789 With StringBuilder class: string7 = 123456789

Notice that for string3 there is a signed decimal representation of the string because we didn’t set a radix into the Integer.toString() method. On the other hand, string4 represents the hex of the integer, because we defined radix to 16.

7. Download the source code

This was an example of Java convert int to string.

Last updated on May 13th, 2021

Photo of Katerina Zamani

Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.