int to String in Java

Converting an integer to a string in Java is important in many cases, such as when you need to display a numerical value to a user or when you need to concatenate multiple values of different types into a single string.

There are several ways to convert an integer to a string in Java:

Using Integer.toString(int) method

This method is the most straightforward way to convert an integer to a string.

int num = 23; String str = Integer.toString(num); System.out.println(str); //Output: 23

Using String.valueOf(int) method

This method is similar to Integer.toString(int) but returns a string representation of the int argument.

int num = 23; String str = String.valueOf(num); System.out.println(str); //Output: 23

Using concatenation

You can also convert an integer to a string by concatenating an empty string with the integer value.

int num = 23; String str = "" + num; System.out.println(str); //Output: 23

Using StringBuilder class

You can also use the StringBuilder class to convert an integer to a string.

int num = 23; String str = new StringBuilder().append(num).toString(); System.out.println(str); //Output: 23

Using String.format() method

You can use the String.format method to convert an integer to a string in Java. The String.format method can be used to format a string by replacing placeholders with actual values. You can use the %d placeholder to format an integer value.

int num = 23; String str = String.format("%d", num); System.out.println(str); //Output: 23

The above code will format the integer 23 as a string and store the result in the str variable. The %d placeholder tells String.format that you want to format an integer value.

Using DecimalFormat class

You can also use the DecimalFormat class to convert an integer to a string in Java. The DecimalFormat class is part of the java.text package and can be used to format decimal numbers, including integers, as strings.

import java.text.DecimalFormat; int num = 23; DecimalFormat df = new DecimalFormat("0"); String str = df.format(num); System.out.println(str); //Output: 23

The above code will create a new DecimalFormat object with the pattern "0". The format method will then format the integer 23 as a string using the specified pattern and store the result in the str variable.

You can specify a different pattern in the constructor of the DecimalFormat class if you need to format the integer in a different way. For example, you can use the pattern "#,##0" to format the integer with commas as thousands separators.

These are the most commonly used methods to convert an integer to a string in Java. Choose the method that best fits your needs and requirements.

What occurs when converting an integer to a string in Java?


Learn to Convert Java Int to String in the Java Programming

When you convert an integer to a string in Java, the integer value is transformed into a string representation of that value. The string representation is simply a sequence of characters that represent the integer value.

For example, if you have the integer value 23, its string representation would be the characters "23".

Conclusion

It's important to note that converting an integer to a string does not change the underlying value of the integer. The original integer value is not modified. The conversion simply creates a new string representation of that value.