I have a method which takes String argument. In some cases I want to pass int value to that method. For invoking that method I want to convert int into String. For that I am doing the following:
aMethod(""+100);
One more option is:
aMethod(String.valueOf(100));
Both are correct. I don't know which is appropriate? Which gives better performance?
Mostly this is happen in GWT. In GWT for setting size of panels and widgets I want to do this.
Using +
on strings creates multiple string instances, so using valueOf
is probably a bit more performant.
Since you're mostly using it in GWT, I'd go with the ""+ method, since it's the neatest looking, and it's going to end up converted to javascript anyway, where there is no such thing as a StringBuilder.
Please don't hurt me Skeet Fanboys ;)
Normally you'd 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 though, IMO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With