String class provides us with the method String.valueOf(some stuff) that basically converts "some stuff" into a string..
But we can also concatenate "some stuff" with an empty String ("").
Both these methods result in a String representation of an object(or other data type) so my question is what is the difference between the two, advantages of one over the other, disadvantages of one over the other and which method do you guys usually use when forced to convert something to a String..

I've never used the valueOf() method. Just use the '+' operator. The + operator creates a new String in the background anyway. The valueOf() method returns a new String so it does the same. Neither method has a time advantage that anyone cares about; although I don't claim to know the details, to create a new String you basically have to copy every character into an array which is O(n) so both methods are O(n).

Actually I have written a program that times those 2 methods and the String.valueOf() method is much faster than the other:


Try this code:

int N = 1000000;

long time = System.currentTimeMillis();
for (int i=0;i<N;i++) {
   String a = ""+i;
}
time = System.currentTimeMillis() - time;
System.out.println(time);


time = System.currentTimeMillis();
for (int i=0;i<N;i++) {
   String a = String.valueOf(i);
}
time = System.currentTimeMillis() - time;
System.out.println(time);

The String.valueOf() creates 1 String. But the '+' symbol: "" + 5 , creates 1 String: "" and then when the '+' is applied it creates one more String which is the result.

In both cases, the String is still created in O(n) time, so the choice of which to use is unimportant. The only time when a developer should bother with something like this is when using one method over another becomes a bottleneck, which in almost all cases, it will not.

Oh, and when I say O(n) here I'm referring to the fact that the operation's completion time is dependent on the number of characters that need to be copied, I'm not referring to the N value from your program.

In both cases, the String is still created in O(n) time, so the choice of which to use is unimportant. The only time when a developer should bother with something like this is when using one method over another becomes a bottleneck, which in almost all cases, it will not.

Oh, and when I say O(n) here I'm referring to the fact that the operation's completion time is dependent on the number of characters that need to be copied, I'm not referring to the N value from your program.

I wrote that piece of code out of curiosity. Why when I execute it the String.valueOf appears to be faster?

Your example convinced me that the valueOf method completes faster; I'm not arguing with you on that. The linear time thing I was talking about is true, but isn't really that important - the main point I wanted to make is that in most cases, the choice of either valueOf or using the '+' operator is really up to the programmer, since either choice will not have a negative effect on the program's run time, as your program demonstrated. (It only took a couple hundred ms for a million operations... the average Java program probably does only a handful of String concatenations, with some exceptions)

Your example convinced me that the valueOf method completes faster; I'm not arguing with you on that. The linear time thing I was talking about is true, but isn't really that important - the main point I wanted to make is that in most cases, the choice of either valueOf or using the '+' operator is really up to the programmer, since either choice will not have a negative effect on the program's run time, as your program demonstrated. (It only took a couple hundred ms for a million operations... the average Java program probably does only a handful of String concatenations, with some exceptions)

You are right, it is up to the programmer. Doing this: "" + 5 is faster to type and easy to read, even though I use the String.valueOf method.

For programs with many concatenations everybody uses the StringBuffer/StringBuilder, so the choice between the '+' or the String.valueOf isn't an issue, because it is not used.

For a single conversion I think that the String.valueOf method is more "formal".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.