I usually use "" + i for convenient. But compare about perfomance myself, I thought String.valueOf(i) will be faster. Is it right? Which one should I use?
I usually use "" + i for convenient. But compare about perfomance myself, I
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Choose what reads best. Then, profile your application before optimizing anything.
Always profile first (for instance with VisualVM since you’re coding in Java).
There are many resources on profiling and optimization. The latest good read I came across is this Step by Step Optimisation by Tony Albrecht.
And if performance really becomes critical, consider switching to StringBuilder.
EDIT:
Well there is no straight answer to your question imho. Micro-benchmarking the performance of
String.valueOf(i)againsti + ""has little value. The real answer comes from benchmarking your application: its very own complexity and memory access pattern decides it all.Those are not my ideas but I’m buying them: stop programming like it’s 1975, big O notation needs an update.