4

So our computer science teacher taught us how to convert ints to Strings by doing this:

int i=0;
String s = i+"";

I guess he taught it to us this way since this was relatively basic computer science (high school class, mind you), but I was wondering if this was in some way "bad" when compared to:

int i=0;
String s = Integer.toString(i);

Is it more costly? Poor habit? Thanks in advance.

John L.
  • 105
  • 5
  • Possible duplicate of http://stackoverflow.com/questions/2506474/is-concatenating-with-an-empty-string-to-do-a-string-conversion-really-that-bad – Ed Staub Sep 29 '11 at 23:55
  • Possible duplicate (this one about `int` specifically, for what it's worth): http://stackoverflow.com/questions/1572708/is-conversion-to-string-using-int-value-bad-practice – Paul Bellora Sep 30 '11 at 04:24

4 Answers4

9

In theory

The instruction

String s = i + "";

does the following: convert i to String by calling Integer.toString(i), and concatenates this string to the empty string. This causes the creation of three String objects, and one concatenation.

The instruction

String s = Integer.toString(i);

does the conversion, and only the conversion.

In practice

Most JIT compilers are able to optimise the first statement to the second one. I wouldn't be surprised if this optimisation is done even before (by the javac compiler, at compilation of your Java source to bytecode)

Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
8
i+""

is equivalent to

new StringBuilder().append(i).append("").toString();

so yes it is less efficient, but unless you are doing that in a loop or such you probably won't notice it.

[edited: from comments by StriplingWarrior-thanks]

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • 3
    +1: That said, I think a lot of people would argue that from a semantic standpoint `Integer.toString(i)` better "describes" the idea that you're trying to convert an integer to a string. `i + ""` says "I want to append an empty string to this integer." – StriplingWarrior Sep 29 '11 at 23:17
  • Agreed with @StriplingWarrior - I would've thought the professor would at least mention `String.valueOf()`. – Paul Bellora Sep 29 '11 at 23:22
  • 4
    actually it's equivalent to `new StringBuilder().append(i).append("").toString();` otherwise you'll just get an empty string with a backing array of length i – ratchet freak Sep 29 '11 at 23:39
  • 1
    Yep ratchet is correct ;) Personally the only reason I wouldn't use it, is that it's a strange way to do this. The static methods are much clearer in what the intent of the code is. Explicit is better than implicit *murmurs python zen* – Voo Sep 30 '11 at 00:03
4

That technique works but it relies on the magic of string concatenation in Java (which is one of the few areas that involves operator overloading) and might be confusing to some.

I find the following techniques to be more clear:

String.valueOf(123); // => "123"
Integer.toString(123); // => "123"

And as others have mentioned, it results in more concise bytecode since it avoids the implicit construction of a new StringBuilder and the accompanying method calls.

maerics
  • 151,642
  • 46
  • 269
  • 291
0

Using that method will compile, since your teacher used that, you might want to use that method when doing stuff in class, but to make it more clean, you might want to use the following code instead.

int i=0;
String s = Integer.toString(s);

Its looks more official....