File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer.toString(i)

 
Kris Reid
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which method is more efficient to get a String from and int


"" + 5;

or

Integer.toString(5);
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends. Do you really mean '5', or a variable holding 5? If you really mean a literal, then "" + 5 will be evaluated at compile time to be "5", so it will be more efficient than Integer.toString(5) which has to do the conversion at runtime.

But if we're talking about a variable holding 5, then the opposite is true: at runtime there will be a StringBuffer to do the catenation, and that will be converted to a String, so the catenation version will do a lot more work.

But you really shouldn't be worrying about such little things, most of the time.
 
Kris Reid
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

I'm writting a swing app that converts a heap of data and I am trying to get as much performance as possible.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your best bet is going to be to write it in a way that is easy to read/maintain, and THEN use a profiler to see where the bottlenecks are. Often they are not where you think they'd be, and you end up spending a lot of time making minor improvements.

Using a profiler will show you exactly where you should be focusing your effort.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic