1

Not confident about whether this will be downvoted or closed... I need expert opinion on this.

The context is in our application, we have written code like :

//countryId is an integer, searchCity() expects two String parameters
loadCity(countryName , countryId + "");

Will it make any difference if I change (I am being forced to do so) the call like :

loadCity(countryName, String.valueOf(countryId));

Or,

loadCity(countryName, Integer.toString(countryId));

Will this make any difference in sense of performance?

Gabe
  • 84,912
  • 12
  • 139
  • 238
tusar
  • 3,364
  • 6
  • 37
  • 60
  • 2
    It's hard to imagine an application where the method of converting an integer to a string has an effect on performance. – Gabe Apr 06 '12 at 13:08
  • 3
    another thread on SO can help you : http://stackoverflow.com/questions/7752347/string-valueof-vs-concatenation-with-empty-string –  Apr 06 '12 at 13:08
  • 2
    possible duplicate of [Integer.toString(int i) vs String.valueOf(int i)](http://stackoverflow.com/questions/3335737/integer-tostringint-i-vs-string-valueofint-i) – Nix Apr 06 '12 at 13:09
  • from the duplicate question I added, concatenation uses String.valueOf() under the covers. – smcg Apr 06 '12 at 13:14

5 Answers5

2

I'd say the main difference is readability. There's no use in micro-benching here. IMHO String#valueOf reads the best.

mre
  • 43,520
  • 33
  • 120
  • 170
2

For the example you have given, the answer will really depend on the type of 'integer' you are using.

loadCity(countryName , countryId + "");

For an Integer object this is equivelent to :

loadCity(countryName, countryId.toString() + "");

Whereas for an int primitive, this code is equivelent to :

loadCity(countryName, String.valueOf(countryId) + "");

In either case, as ArjunShankar pointed out there is a good chance that the compiler has optimised your code anyway. So if your question is 'do I go back and refactor all my code?', then I would say 'don't sweat the small stuff'. But in the future use a more conventional approach to avoid the down votes.

klonq
  • 3,535
  • 4
  • 36
  • 58
  • +1 for saying those two are equivalent. `String` class reference says _String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java._ – ArjunShankar Apr 06 '12 at 13:12
  • 1
    When compiling to bytecode, my bet is `+ ""` gets optimized away. So these two should be equivalent to `loadCity (countryName, countryId.toString);` – ArjunShankar Apr 06 '12 at 13:13
  • thanks for the tip @ArjunShankar . And klonq , you are the only person who understood the scenario. Accepting it. – tusar Apr 08 '12 at 06:56
1

From the docs of String.valueOf

"The representation is exactly the one returned by the Integer.toString method of one argument."

I would use String.valueOf because you can use it on more then just Integers, i.e. you don't have to know whether you have an int, double, bool, etc....

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

They are same.... because the method String.valueOf(int i) implicitly calls Integer.toString(i, 10);

Nagaraju Badaeni
  • 890
  • 8
  • 14
0

A search on both methods informed me all of these are equivalent:

    String.valueOf(countryId) 
    Integer.toString(countryId) 
    countryId.toString() //Only if countryId is an Integer

Because they all call:

    Integer.toString(countryId, 10) 

The only difference is if you wish to use a different radix, ie:

   Integer.toString(countryId, radix)

Personally I think countryId.toString() reads better when using an Integer. Otherwise Integer.toString(countryId) is the way to go. But that is just my personal opinion. Performance-wise you should use Integer.toString(countryId, 10).

I think that adding an empty string to an int to convert it to a String is a bad practice.

Wouter Verleur
  • 61
  • 2
  • 14