• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How does String being immutable improves performance?

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who said it improves performance? And more specifically, what did they mean by that?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. :)
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic