Wednesday, June 8, 2005

Converting an int to a String

WARNING: This blog entry was imported from my old blog on (which used different blogging software), so formatting and links may not be correct.

I came across
this tip in a blog referenced from
Eric's linkblog.

The author mentions that you can convert from an int to a String like this:

int one = 1;
String str = one + "";

And he calls that cute!

I disagree - I think that's ugly (and as he points out it's less efficient).

There's a clean way to do this, not listed in his blog:

int one = 1;
String str = Integer.toString(one);

Nice efficient bytecode too:

0: iconst_1
1: istore_1
2: iload_1
3: invokestatic #2; //Method java/lang/Integer.toString:(I)Ljava/lang/String;
6: astore_2
7: return

and there are variations of the method which lets you convert using another base - so you
can convert to hex using Integer.toString(one, 16) for example.

(String.valueOf(int) will turn around and call the above method by the way.)


  1. I do like the concatenation because, assuming the line compiles, everyone understands what it does and you don't need to remember an API. I don't see a big performance hit either. But I can see why you don't and your approach is certainly more "elegant".


  2. Well, the most elegant and logical would be
    int foo = 1;

    String text = foo.toString();

    but that doesn't work, of course. AutoBoxing allows to write somehow similar code:
    String fooStr = ((Integer)foo).toString();
    Well... that's neither elegant nor efficient (if foo is outside the cached range of values).

    I think this is a bit of an oversight in the Java language design, the same with arrays, where you can call array.toString, array.length, but not array.copy... Just more inconsistencies to remember...

    On weirdness with primitives and Strings
    If you're a new dev and find

    boolean Boolean.getBoolean(String)

    expect the unexpected...

  3. When you write String str = myOtherString + x + " static string"; the compiler should be smart enough to turn it into effecient code using StringBuilder. On that same note, shouldn't the compiler be smart enough to turn String str = 0 + ""; into the appropriate bytecode since this shortcut has been around for years?

  4. Thank you all for your comments.
    Let me just qualify what I meant.
    Clearly, I enjoy type conversion when it's used logically, like this:
    int count = 10;
    String message = "Your basket contains " + count + " items";
    This code is readable. And -that- is my number one goal: clear and readable code. Even if that code is harder to construct in the first place - requiring programmer knowledge of available APIs, or requiring well thought out variable names. I wish I hadn't included the bytecode dump, since from that perspective, choosing shorter names is also a performance advantage - smaller class footprint, faster symbol lookup etc.
    Code readability is very important to me. And if you're -just- doing an integer conversion, then
    String s = i + "";
    is mysterious - a reader not familiar with this pattern will be slightly puzzled.
    String s = Integer.toString(i);
    will immediately be able to read this as an integer string conversion.
    So yeah, I agree with your comments that the compilers should optimize all these examples down to the essential bytecodes, but at the end of the day, my objection is with the resulting .java code.