String conversions

I have a method that takes a String argument. In some cases, I want to pass an int value to this method. To call this method, I want to convert int to String. For this I do the following

aMethod(""+100); 

Another option

  aMethod(String.valueOf(100)); 

Both are correct. I do not know what is suitable? What gives the best performance?

In most cases, this happens in the GWT. In GWT, I want to do this to adjust the size of panels and widgets.

Can someone give a suggestion?

+6
java string
source share
5 answers

Since you mainly use it in GWT, I would go with the β€œ+” method, since it was the most neat, and in any case it will turn into javascript where there is no such thing as StringBuilder.

Please do not harm me Skeet Fanboys;)

+2
source share

A good article by John Skeet about your subject matter: String conversions - good, well-suppose, and terribly terrible

+11
source share

Using + in strings creates multiple instances of strings, so using valueOf is probably a bit more efficient.

+4
source share

Usually you should use Integer.toString (int) or String.valueOf (int). They both return the same thing, and probably have identical implementations. Integer.toString (int) is a little easier to read at a glance, although IMO.

+1
source share

I would suggest that this is:

 aMethod(""+100); 

turns this into a compiler:

 aMethod(new StringBuilder("").append(String.valueOf(100)).toString()); 

Thus, the choice of calling String.valueOf directly is probably the best choice. You can always compile them and compare bytecode to verify this.

0
source share

All Articles