How to Convert int to String in Java

java dev.to

While working with Java, you will often need to convert numeric values into text. A common example is converting an int value into a String.

This is useful when working with string concatenation, displaying numbers, storing values as text, or passing data to methods that expect a String.

In this article, we'll explore several simple ways to convert an int to a String in Java.

1. Using String.valueOf()

One of the simplest and most commonly used approaches is String.valueOf().

public class Main {
public static void main(String[] args) {
int number = 100;

    String result = String.valueOf(number);

    System.out.println(result);
}
Enter fullscreen mode Exit fullscreen mode

}
Output
100

String.valueOf() converts the integer value into its string representation.

2. Using Integer.toString()

You can also use the toString() method provided by the Integer class.

public class Main {
public static void main(String[] args) {
int number = 250;

    String result = Integer.toString(number);

    System.out.println(result);
}
Enter fullscreen mode Exit fullscreen mode

}
Output
250

This approach makes the conversion explicit by using the Integer wrapper class.

3. Using String Concatenation

Another simple technique is concatenating the integer with an empty string.

public class Main {
public static void main(String[] args) {
int number = 500;

    String result = number + "";

    System.out.println(result);
}
Enter fullscreen mode Exit fullscreen mode

}
Output
500

Java automatically converts the integer to a string during concatenation.

Although this works, String.valueOf() or Integer.toString() generally makes your intention clearer and is preferable when conversion is the main goal.

4. Using String.format()

You can also convert an integer into a string using String.format().

public class Main {
public static void main(String[] args) {
int number = 75;

    String result = String.format("%d", number);

    System.out.println(result);
}
Enter fullscreen mode Exit fullscreen mode

}
Output
75

This approach can be useful when you are already formatting multiple values.

For example:

int age = 20;
String message = String.format("Age: %d", age);

System.out.println(message);

Output:

Age: 20

5. Converting an int While Building a String

A common real-world situation is adding an integer to a larger message.

int score = 95;

String message = "Your score is " + score;

System.out.println(message);

Output:

Your score is 95

Java automatically performs the necessary conversion when a primitive value is concatenated with a String.

Why Convert int to String?

There are several situations where this conversion is useful:

Displaying numbers as text
Building messages
Creating formatted output
Working with APIs that require strings
Combining numbers with other text
Storing numeric values in string-based data structures

For example, suppose an application receives an integer ID but needs to construct a URL or message using that value. Converting the number to a string makes that operation straightforward.

Important Point

An int is a primitive data type, while String is an object in Java. Java provides convenient methods such as String.valueOf() and Integer.toString() to handle this conversion.

You don't need to manually manipulate the digits of the number.

Conclusion

Converting an int to a String is a common operation in Java and can be done in several ways. String.valueOf() and Integer.toString() are clear choices for explicit conversion, while string concatenation is convenient when you're already constructing text.

Understanding these approaches will help you handle numbers more effectively when working with strings and Java applications.

Source: dev.to

arrow_back Back to Tutorials