Convert int to String in Java (with Performance Comparison)
π Java Fun Zone: Converting int
to String
Like a Pro! πβ
π₯ The Missionβ
You're coding away, deep in thought, and suddenly you need to convert an int
to a String
. Panic? NEVER! Java has got your back with multiple ways to do it. But which one is the best? Let's dive in and find out! π‘
π οΈ The Mighty Conversion Techniquesβ
Java provides a bunch of ways to transform your humble int
into a shiny String
. Here are our contenders:
Integer.toString()
π― (Fastest & Recommended!)String.valueOf()
String.format()
- String Template Magic (Java 21)
A little Java snippet to set the stage:
int num = 100;
String value1 = Integer.toString(num); // Works for Integer too
String value2 = String.valueOf(num); // Works for Integer too
π₯ 1. The Champion: Integer.toString()
β
The Integer.toString(int)
method takes an int
and returns a String
representation of it. Simple and blazing fast. β‘
Assertions.assertEquals("0", Integer.toString(0));
Assertions.assertEquals("40", Integer.toString(40));
Assertions.assertEquals("-40", Integer.toString(-40));
β οΈ Beware of the Limitsβ
Java int
values range from -2,147,483,648 to 2,147,483,647. Go beyond this, and you're in for a surprise! π²
Assertions.assertEquals("2147483647", Integer.toString(Integer.MAX_VALUE));
Assertions.assertEquals("-2147483648", Integer.toString(Integer.MIN_VALUE));
π Fun with Different Basesβ
Want to see your int
in different numeral systems? No problem!
Assertions.assertEquals("101000", Integer.toBinaryString(40)); // Binary
Assertions.assertEquals("50", Integer.toOctalString(40)); // Octal
Assertions.assertEquals("28", Integer.toHexString(40)); // Hex
π
2. String.valueOf()
β A Strong Contenderβ
Works just like Integer.toString()
, but calls it internally. Essentially, it's the same thing wearing different clothes. π€·ββοΈ
Assertions.assertEquals("0", String.valueOf(0));
Assertions.assertEquals("40", String.valueOf(40));
Assertions.assertEquals("-40", String.valueOf(-40));
πΆββοΈ 3. String.format()
β Slowpoke Alert! π’β
You could use String.format()
, but why bring a bazooka to a knife fight? πΉ
String formatted = String.format("%d", num); // Works but slow π
π 4. String Templates (Java 21+) β The Cool New Kid πβ
With Java 21, we now have string templates!
String s = STR."\{num}";
Looks cool, performs decently, but still not the fastest.
βοΈ Performance Battle Royale π₯β
We put these methods to the test using JMH (Java Microbenchmark Harness). Hereβs how they performed:
Method | Syntax | Performance (Avg Time per Operation) |
---|---|---|
π Integer.toString() | Integer.toString(int) | 0.012 Β΅s β‘ |
π₯ String.valueOf() | String.valueOf(int) | 0.016 Β΅s |
π₯ String.format() | String.format("%d", int) | 0.0143 Β΅s π’ |
π© String template | STR."\{i}" | 0.019 Β΅s |
π¬ Why the Difference?β
Here's a sneak peek at the bytecode behind these methods:
Integer.toString()
: Calls a lightweight static method. ποΈString.valueOf()
: CallsInteger.toString()
internally. πββοΈString.format()
: Uses varargs + boxing. Slow! πString template
: UsesInvokeDynamic
, but still a tad slower. π€·ββοΈ
π― Conclusion: The Winner Isβ¦ πβ
For converting int
to String
, use:
β
Integer.toString(int)
β Fastest and best!
β
String.valueOf(int)
β A close second.
π« Avoid String.format()
unless you love unnecessary performance hits. π€¦ββοΈ
π String templates are fun, but not the best for performance-critical applications.
π Happy Codingβ
Now go forth and convert those integers like a Java Ninja! π₯·π»π₯
βοΈ Tested on an 11th Gen Intel i5-1135G7, Windows 11, Java 21.