Denis Palnitsky Denis Palnitsky - 6 months ago 12
Java Question

How do I convert from int to String?

I'm working on a project where all conversions from

are done like this:

int i = 5;
String strI = "" + i;

I'm not familiar with Java. Is this usual practice or is something wrong, as I suppose?


Normal ways would be Integer.toString(i) or String.valueOf(i).

The concatenation will work, but it is unconventional and could be a bad smell as it suggests the author doesn't know about the two methods above (what else might they not know?).

Java has special support for the + operator when used with strings (see the documentation) which translates the code you posted into:

StringBuilder sb = new StringBuilder();
String strI = sb.toString();

at compile-time. It's slightly less efficient (sb.append() ends up calling Integer.getChars(), which is what Integer.toString() would've done anyway), but it works.

To answer Grodriguez's comment: ** No, the compiler doesn't optimise out the empty string in this case - look:

simon@lucifer:~$ cat
public class TestClass {
  public static void main(String[] args) {
    int i = 5;
    String strI = "" + i;
simon@lucifer:~$ javac && javap -c TestClass
Compiled from ""
public class TestClass extends java.lang.Object{
public TestClass();
   0:    aload_0
   1:    invokespecial    #1; //Method java/lang/Object."<init>":()V
   4:    return

public static void main(java.lang.String[]);
   0:    iconst_5
   1:    istore_1

Initialise the StringBuilder:

   2:    new    #2; //class java/lang/StringBuilder
   5:    dup
   6:    invokespecial    #3; //Method java/lang/StringBuilder."<init>":()V

Append the empty string:

   9:    ldc    #4; //String
   11:    invokevirtual    #5; //Method java/lang/StringBuilder.append:

Append the integer:

   14:    iload_1
   15:    invokevirtual    #6; //Method java/lang/StringBuilder.append:

Extract the final string:

   18:    invokevirtual    #7; //Method java/lang/StringBuilder.toString:
   21:    astore_2
   22:    return

There's a proposal and ongoing work to change this behaviour, targetted for JDK 9.