问题
Once I studied about the advantage of a string being immutable because of something to improve performace in memory.
Can anybody explain this to me? I can\'t find it on the Internet.
回答1:
Immutability (for strings or other types) can have numerous advantages:
- It makes it easier to reason about the code, since you can make assumptions about variables and arguments that you can't otherwise make.
- It simplifies multithreaded programming since reading from a type that cannot change is always safe to do concurrently.
- It allows for a reduction of memory usage by allowing identical values to be combined together and referenced from multiple locations. Both Java and C# perform string interning to reduce the memory cost of literal strings embedded in code.
- It simplifies the design and implementation of certain algorithms (such as those employing backtracking or value-space partitioning) because previously computed state can be reused later.
- Immutability is a foundational principle in many functional programming languages - it allows code to be viewed as a series of transformations from one representation to another, rather than a sequence of mutations.
Immutable strings also help avoid the temptation of using strings as buffers. Many defects in C/C++ programs relate to buffer overrun problems resulting from using naked character arrays to compose or modify string values. Treating strings as a mutable types encourages using types better suited for buffer manipulation (see StringBuilder in .NET or Java).
回答2:
Consider the alternative. Java has no const qualifier. If String objects were mutable, then any method to which you pass a reference to a string could have the side-effect of modifying the string. Immutable strings eliminate the need for defensive copies, and reduce the risk of program error.
回答3:
Immutable strings are cheap to copy, because you don't need to copy all the data - just copy a reference or pointer to the data.
Immutable classes of any kind are easier to work with in multiple threads, the only synchronization needed is for destruction.
回答4:
a) Imagine StringPool facility without making string immutable , its not possible at all because in case of string pool one string object/literal e.g. "Test" has referenced by many reference variables , so if any one of them change the value others will be automatically gets affected i.e. lets say
String A = "Test" and String B = "Test"
Now String B called "Test".toUpperCase() which change the same object into "TEST" , so A will also be "TEST" which is not desirable.
b) Another reason of Why String is immutable in Java is to allow String to cache its hashcode , being immutable String in Java caches its hash code and do not calculate every time we call hashcode method of String, which makes it very fast as hashmap key.
回答5:
Think of various strings sitting on a common pool. String variables then point to locations in the pool. If u copy a string variable, both the original and the copy shares the same characters. These efficiency of sharing outweighs the inefficiency of string editing by extracting substrings and concatenating.
回答6:
Perhaps, my answer is outdated, but probably someone will found here a new information.
Why Java String is immutable and why it is good:
- you can share a string between threads and be sure no one of them will change the string and confuse another thread
- you don’t need a lock. Several threads can work with immutable string without conflicts
- if you just received a string, you can be sure no one will change its value after that
- you can have many string duplicates – they will be pointed to a single instance, to just one copy. This saves computer memory (RAM)
- you can do substring without copying, – by creating a pointer to an existing string’s element. This is why Java substring operation implementation is so fast
- immutable strings (objects) are much better suited to use them as key in hash-tables
回答7:
Fundamentally, if one object or method wishes to pass information to another, there are a few ways it can do it:
It may give a reference to a mutable object which contains the information, and which the recipient promises never to modify.
It may give a reference to an object which contains the data, but whose content it doesn't care about.
It may store the information into a mutable object the intended data recipient knows about (generally one supplied by that data recipient).
It may return a reference to an immutable object containing the information.
Of these methods, #4 is by far the easiest. In many cases, mutable objects are easier to work with than immutable ones, but there's no easy way to share with "untrusted" code the information that's in a mutable object without having to first copy the information to something else. By contrast, information held in an immutable object to which one holds a reference may easily be shared by simply sharing a copy of that reference.
来源:https://stackoverflow.com/questions/3407403/whats-the-advantage-of-a-string-being-immutable