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?

4
  • 2
    It's hard to imagine an application where the method of converting an integer to a string has an effect on performance. Commented Apr 6, 2012 at 13:08
  • 3
    another thread on SO can help you : stackoverflow.com/questions/7752347/… Commented Apr 6, 2012 at 13:08
  • 2
    possible duplicate of Integer.toString(int i) vs String.valueOf(int i) Commented Apr 6, 2012 at 13:09
  • from the duplicate question I added, concatenation uses String.valueOf() under the covers. Commented Apr 6, 2012 at 13:14

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+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.
When compiling to bytecode, my bet is + "" gets optimized away. So these two should be equivalent to loadCity (countryName, countryId.toString);
thanks for the tip @ArjunShankar . And klonq , you are the only person who understood the scenario. Accepting it.
2

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

Comments

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....

Comments

0

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.