# 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](https://www.ionos.com/en-ie/digitalguide/websites/web-development/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](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-primitives/). Integer variables can only contain whole numbers between -2147483648 and 2147483647. [Java strings](https://www.ionos.com/en-ie/digitalguide/websites/web-development/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](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-string-to-int/), check out the article in our Digital Guide.

## 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:

```Java
Integer.toString(Integer);
```

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:

```java
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");
	}
}
```

The output will look like this:

```java
There are 5 cars in the car park
```

## 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:

```java
String.valueOf(Integer);
```

The example outlined above will look like this:

```java
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");
	}
}
```

The output looks as follows:

```java
There are 5 cars in the car park
```

## 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:

```java
String.format(placeholder, integer);
```

The code will then look like this:

```java
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");
	}
}
```

And we again get the output:

```java
There are 5 cars in the car park
```

## How to link an integer to an empty string

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:

```java
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");
	}
}
```

The output once again looks as follows:

```java
There are 5 cars in the car park
```

## 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:

```java
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");
	}
}
```

The output looks the same as above:

```java
There are 5 cars in the car park
```


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-int-to-string/](https://www.ionos.com/en-ie/digitalguide/websites/web-development/java-int-to-string/) for AI/LLM consumption.