Home » Java Basics » data types » primitives » Java convert int to string Example

About Katerina Zamani

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.

Java convert int to string Example

Integer to String conversion is a basic task in many Java projects. There are many ways to parse from int to string, 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.

1. Syntax of Integer.toString() method

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

The parameters of this method are:

  • i: the integer to be converted.
  • radix: the used base – number system in order to represent the string.

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

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. Syntax of 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. Syntax of 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:

  • l: the Local to be addressed during the formatting.
  • format: the format string, which includes at least one format specifier and sometimes a fixed text.
  • args: arguments that refer to the format specifiers we set in the format parameter.

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 like array with sequence of characters. To add a new argument to the end of the string, StringBuilder instance implements append() method. At the end it is important to call toString() method, in order to take the string representation of the data in this sequence.

5. Example of Integer to String conversion

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


package com.javacodegeeks.javabasics.inttostring;

import java.util.IllegalFormatException;

public class IntToStringTest {

	public static void main(String args[]) {
		int mainInt = 123456789;
		// add + operator
		String string1 = "" + mainInt;
		System.out.println("With + operator: string1 = " + string1);
		// into the "" string
		String string2 = "123";
		System.out.println("Directly in the String: string2 = " + string2);
		// use Integer.toString() with and without radix
		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.valueOf() method
		String string5 = String.valueOf(mainInt);
		System.out.println("With valueOf method: string5 = " + string5);
			// use format() method
			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 instance
		StringBuilder sb = new StringBuilder();
		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.


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.

Download the source code

This was an example of Java convert int to string. Download the source code of this example: IntToStringTest.zip

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

Notify of