How To Convert int to String in Java
0 CommentsLast Updated on October 22, 2024 by jt
Converting an int
to a String
is a common task in Java, especially when working with user interfaces, file output, or JSON formatting. Java provides several ways to perform this conversion, each with different use cases and characteristics.
In this blog post, I will cover the most popular methods for converting int
to String
and discuss when to use each one.
int to String Using String.valueOf()
The most commonway to convert an int
to a String
is by using String.valueOf()
. This method converts the primitive int
to the corresponding String representation.
Example:
int number = 123;
String str = String.valueOf(number);
System.out.println("Converted string: " + str);
-----------output-------------
Converted string: 123
The String.value()
of method will also work with the Integer
wrapper class for int
, and is also null safe. Meaning you can pass a null value without having an exception thrown.
int to String using Integer.toString()
We can also convert an int
to a String
is by using the Integer.toString()
method.
Example:
int number = 456;
String str = Integer.toString(number);
System.out.println("Converted string: " + str);
-------------output-------------
Converted string: 456
int to String Using String Concatenation
String concatenation (+
) is another way to convert an int
to a String
. When concatenating an int
with an empty string, Java implicitly converts the int
to a String
.
Example:
int number = 789;
String str = number + "";
System.out.println("Converted string: " + str);
-------------output----------------
Converted string: 789
int to String Using String.format()
If you need to format the output, String.format()
can be used. It allows for greater control over the resulting string You can read about the versatility of String.format() in this post.
int to String with Number Base Using Integer.toString(int, radix)
If you need to convert an int
into a string with a specific base (i.e. binary, octal, or hexadecimal), Integer.toString(int, radix)
can be used. This method allows you to specify the base (or radix) for the conversion.
Example:
int number = 255;
String binaryString = Integer.toString(number, 2); // Convert to binary
String hexString = Integer.toString(number, 16); // Convert to hexadecimal
System.out.println("Binary string: " + binaryString);
System.out.println("Hexadecimal string: " + hexString);
---------------output----------------
Binary string: 11111111
Hexadecimal string: ff
Best Practices
- Use
String.valueOf()
for general-purpose conversions: It’s the most versatile and handles null values effectively. - Avoid string concatenation for production code: While fast, it’s less clear and creates unnecessary string objects.
- Use
String.format()
only when formatting is necessary: It’s overkill for simple conversions but valuable for more complex formatting. - Choose the right radix for specific use cases: If you’re working with binary or hexadecimal data, use
Integer.toString(int, radix)
for precise control.
Conclusion
Converting an int
to a String
in Java is straightforward, with several methods available depending on your needs. While String.valueOf()
and Integer.toString()
are the most common approaches, methods like String.format()
and Integer.toString(int, radix)
provide additional flexibility for formatting or base conversions. When you need to convert an int
to a String
, Java has the tools for your use case!
If you need to convert a String to an int in Java, checkout this post for How to Convert a String to int in Java!
As always, you can find the source code for this post in my Github repository here.