How to convert an int to a string in Java

If you want to convert a Java int to a string, there are five options. We show you the different ways.

What is int to string conversion in Java?

If you work with the programming language Java, you’ll encounter the various Java data types and will at some point need to convert an int to a string. Integers (int) are one of the primitive Java data types. Integer variables can only contain whole numbers between -2147483648 and 2147483647. Java strings, on the other hand, are a complex data type that can contain letters, digits and special characters. If you want to convert an int into a string, you have five options. We’ll introduce each of them to you below. The options are:

  • The method Integer(int).toString from the integer class
  • The method valueOf() from the string class
  • The method String.format() from the string class
  • Adding the int an empty string
  • The class DecimalFormat

If you want to know how to do the reverse and convert a string to an integer, check out the article in our Digital Guide.

Web Hosting
Secure, reliable hosting for your website
  • 99.9% uptime and super-fast loading
  • Advanced security features
  • Domain and email included

How to use Integer.toString()

One of the simplest and most practical methods for converting a Java int to a string is the method Integer(int).toString. The basic syntax of this method looks as follows:

Integer.toString(Integer);
Java

With this method, the integer is converted and returned as a string instance. In the example below we create an integer named ‘amount’ and assign it the value 5. Afterwards we enter it as a parameter in the method Integer.toString(). We assign this new string value to the string variable VEHICLES. We’ll then combine this string with other strings and use the Java command System.out.println for output. The code for all of this looks as follows:

public class Main {
	public static void main(String[] args) {
	int amount = 5;
	String VEHICLES = Integer.toString(amount);
	System.out.println("There are " + VEHICLES + " cars in the car park");
	}
}
java

The output will look like this:

There are 5 cars in the car park
java

How to use String.valueOf()

The method String.valueOf() works similarly and enables you to quickly convert an int into a string in Java. To use it, we’ll first create an integer with the name ‘amount’ and enter it as a parameter into String.valueOf(). The basic syntax looks as follows:

String.valueOf(Integer);
java

The example outlined above will look like this:

public class Main {
	public static void main(String[] args) {
	int amount = 5;
	String VEHICLES = String.valueOf(amount);
	System.out.println("There are " + VEHICLES + " cars in the car park");
	}
}
java

The output looks as follows:

There are 5 cars in the car park
java

How to use String.format()

The next method is a bit less direct but also works well. It has two parameters, a variable like in the above examples and the placeholder ‘%d’. The placeholder is used to format the strings and stands in for a whole number. We’ll again name the variable ‘amount’ and convert it into the string ‘VEHICLES’. The basic syntax for String.format() looks as follows:

String.format(placeholder, integer);
java

The code will then look like this:

public class Main {
	public static void main(String[] args) {
	int amount = 5;
	String VEHICLE = String.format("%d", amount);
	System.out.println("There are " + VEHICLES + " cars in the car park");
	}
}
java

And we again get the output:

There are 5 cars in the car park
java

If you want to do an int to string conversion in Java with an empty string, you’ll need the operator +. You can use + to add the integer to an empty string and set the result as a string. Let’s look at an example:

public class Main {
	public static void main(String[] args) {
	int amount = 5;
	String VEHICLES = "" + amount;
	System.out.println("There are " + VEHICLES + " cars in the car park");
	}
}
java

The output once again looks as follows:

There are 5 cars in the car park
java

How to use DecimalFormat

Our last option for converting a Java int to a string uses the class DecimalFormat and requires a few extra steps. First the class needs to be imported. Then we can create the int variable ‘amount’. Next we need a new object for the DecimalFormat class, which we name ‘NewFormat’. Finally we use the method format() to convert amount into a string. The code looks as follows:

import java.text.DecimalFormat;
public class Main {
	public static void main(String[] args) {
	int amount = 5;
	DecimalFormat NewFormat = new DecimalFormat("#");
	String VEHICLE = NewFormat.format(amount);
	System.out.println("There are " + VEHICLES + " cars in the car park");
	}
}
java

The output looks the same as above:

There are 5 cars in the car park
java
Was this article helpful?
Page top