Katerina Zamani

About 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 to 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.

IntToStringTest.java:

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);
		
		try{
			// 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();
		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.

Download the source code

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

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books