Forums Register Login

How does String being immutable improves performance?

+Pie Number of slices to send: Send
There are 2 advantages of String being immutable:
1. Thread Safety (No problem with this)
2. Performance

I am not understanding how does immutable feature of String helps in getting better performance.When everytime a String is modified a new String Object is created and if we do these modifications many times , then more and more String objects will be created in the memory(or more specifically heap).
Then how does this improve memory efficiency or performance?
+Pie Number of slices to send: Send
Who said it improves performance? And more specifically, what did they mean by that?
1
+Pie Number of slices to send: Send
Assuming there is any actual performance gain, it's probably because we can pass around references to existing Strings without worrying that the String will be modified. So we don't need to make a defensive copy first. There may also be some lower-level optimizations the hotspot runtime can make, but I think that would be a secondary consideration.
+Pie Number of slices to send: Send
 

Paul Clapham wrote:Who said it improves performance? And more specifically, what did they mean by that?


I saw it was mentioned by one of the members on stackoverflow, but i could not understand it.
Here is the link:
bitly.com/duDKEy
See LordOfThePigs reply.
+Pie Number of slices to send: Send
Then why didn't you just ask that question, with that reference? It was really a waste of time to replace it by such a vague question.
+Pie Number of slices to send: Send
 

Paul Clapham wrote:Then why didn't you just ask that question, with that reference? It was really a waste of time to replace it by such a vague question.


Because i wanted to ask how does it improve performance when more and more strings are created in the heap.If i just wanted to know how does that improve performance then your argument would have been valid.
+Pie Number of slices to send: Send
 

Rohan Deshmkh wrote:

Paul Clapham wrote:Then why didn't you just ask that question, with that reference? It was really a waste of time to replace it by such a vague question.


Because i wanted to ask how does it improve performance when more and more strings are created in the heap.If i just wanted to know how does that improve performance then your argument would have been valid.



Not to belabor the point, but you could have both provided the context initially and added your own specific query about creating additional objects. Then we all would have had the full relevant picture from the beginning.

Note that nobody's trying to pick on you here. We simply want to encourage clear communication of all the relevant information so that the discussion can be as effective as possible, focusing on the technical matter at hand rather than on meta questions about what the question is. :)
+Pie Number of slices to send: Send
 

Rohan Deshmkh wrote:Because i wanted to ask how does it improve performance when more and more strings are created in the heap.If i just wanted to know how does that improve performance then your argument would have been valid.


Because, like me, Paul probably suspects that you've misunderstood whatever you've "read" about 'performance gain'. Yes, there is a definite performace gain in some circumstances (for example, String.substring()), because immutable classes can often share internals, BUT it's usually also offset by the fact that you need to create a new object every time you want to do something.

Winston
+Pie Number of slices to send: Send
It's ironic, really... the technique touted in that Stack Overflow answer which you referred to? It was recently abandoned by the writers of the standard Java classes, apparently because of the reasons outlined in the comments on that answer.

So you might want to reread the answer, along with the comments. If there's something specific you don't understand about it, then ask that specific question. It's really quite an instructive answer-and-comments set, in that it ends up denying the concept of "performance" as a one-dimensional measure of goodness.
+Pie Number of slices to send: Send
 

Jeff Verdegan wrote:

Rohan Deshmkh wrote:

Paul Clapham wrote:Then why didn't you just ask that question, with that reference? It was really a waste of time to replace it by such a vague question.


Because i wanted to ask how does it improve performance when more and more strings are created in the heap.If i just wanted to know how does that improve performance then your argument would have been valid.



Not to belabor the point, but you could have both provided the context initially and added your own specific query about creating additional objects. Then we all would have had the full relevant picture from the beginning.

Note that nobody's trying to pick on you here. We simply want to encourage clear communication of all the relevant information so that the discussion can be as effective as possible, focusing on the technical matter at hand rather than on meta questions about what the question is. :)



@ apologies to paul and everyone for this.
+Pie Number of slices to send: Send
 

Paul Clapham wrote:It's ironic, really... the technique touted in that Stack Overflow answer which you referred to? It was recently abandoned by the writers of the standard Java classes, apparently because of the reasons outlined in the comments on that answer.

So you might want to reread the answer, along with the comments. If there's something specific you don't understand about it, then ask that specific question. It's really quite an instructive answer-and-comments set, in that it ends up denying the concept of "performance" as a one-dimensional measure of goodness.


Ok i will read the entire thread again and will ask if i have any doubts.
+Pie Number of slices to send: Send
After reading that thread again it look likes there isn't much performance gain.And If there is ,then it is because copying is cheap as you don't need to copy entire data but only the reference to that data.
+Pie Number of slices to send: Send
Yes, I think that's right. If I were writing a class like that for my own use, I probably wouldn't bother with such an optimization, until I found that I really needed it.

But the people who wrote the standard API classes were writing code for the whole world to use. And not only that, back in 1997 they were working in an environment full of magazine articles and web pages which said "Java is slow". So given that environment, they were under pressure to produce performance improvements. The feature about sharing the underlying character array may have been implemented for that reason, but that's just me speculating.
+Pie Number of slices to send: Send
 

Paul Clapham wrote:
But the people who wrote the standard API classes were writing code for the whole world to use. And not only that, back in 1997 they were working in an environment full of magazine articles and web pages which said "Java is slow". So given that environment, they were under pressure to produce performance improvements. The feature about sharing the underlying character array may have been implemented for that reason, but that's just me speculating.



To be fair, object creation and GC were bigger CPU hogs in the early days of Java, so there may have been a valid technical justification for it.
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 910 times.
Similar Threads
Passing by value vs. reference
Creating our own String class?
String Objects created
Strings are immutrable?
string function problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:45:44.