I meant to say to put the String s = ""; outside, and s = ""; inside. Not sure what optimizations exist, but it can't be more expensive than continuously reallocating the memory.
I guess it's a good thing that I'm not paid to do Java at this point.
It's really a technicality and not a big deal at all! I was joking. But your way would be ideal on an embedded system. Or even better, a ring buffer. For Java though, I'm not sure how it all works behind the scenes and if one way truly is better than the other.
The reason why it would cause a problem on a low level system is that there isn't garbage collection. It would probably lead to a memory leak due to constantly reallocating the memory. Depends on how the compiler optimizes and such.
View all comments
Show parent comment
3
u/amoliski Jan 17 '14
...
whoops.
I meant to say to put the String s = ""; outside, and s = ""; inside. Not sure what optimizations exist, but it can't be more expensive than continuously reallocating the memory.
I guess it's a good thing that I'm not paid to do Java at this point.