JAVA —— String is immutable. What exactly is the meaning? [duplicate]
2017-12-15 10:11
609 查看
question:
I wrote the following code on immutable Strings.
Output:
Here the value of variable
source : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I wrote the following code on immutable Strings.
public class ImmutableStrings { public static void main(String[] args) { testmethod(); } private static void testmethod() { String a = "a"; System.out.println("a 1-->" + a); a = "ty"; System.out.println("a 2-->" + a); } }
Output:
a 1-->a a 2-->ty
Here the value of variable
ahas been changed (while many say that contents of the immutable objects cannot be changed). But what exactly does one mean by saying
Stringis immutable? Could you please clarify this topic for me?
source : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Before proceeding further with the fuss of immutability, let's just take a look into the Stringclass and its functionality a little before coming to any conclusion. This is how Stringworks: String str = "knowledge"; This, as usual, creates a string containing "knowledge"and assigns it a reference str. Simple enough? Lets perform some more functions: String s = str; // assigns a new reference to the same string "knowledge" Lets see how the below statement works: str = str.concat(" base"); This appends a string " base"to str. But wait, how is this possible, since Stringobjects are immutable? Well to your surprise, it is. When the above statement is executed, the VM takes the value of String str, i.e. "knowledge"and appends " base", giving us the value "knowledge base". Now, since Strings are immutable, the VM can't assign this value to str, so it creates a new Stringobject, gives it a value "knowledge base", and gives it a reference str. An important point to note here is that, while the Stringobject is immutable, its reference variable is not. So that's why, in the above example, the reference was made to refer to a newly formed Stringobject. At this point in the example above, we have two Stringobjects: the first one we created with value "knowledge", pointed to by s, and the second one "knowledge base", pointed to by str. But, technically, we have three Stringobjects, the third one being the literal "base"in the concatstatement. Important Facts about String and Memory usageWhat if we didn't have another referencesto "knowledge"? We would have lost that String. However, it still would have existed, but would be considered lost due to having no references. Look at one more example below String s1 = "java"; s1.concat(" rules"); System.out.println("s1 refers to "+s1); // Yes, s1 still refers to "java" What's happening: The first line is pretty straightforward: create a new String "java"and refer s1to it. Next, the VM creates another new String "java rules", but nothing refers to it. So, the second Stringis instantly lost. We can't reach it. The reference variable s1still refers to the original String "java". Almost every method, applied to a Stringobject in order to modify it, creates new Stringobject. So, where do these Stringobjects go? Well, these exist in memory, and one of the key goals of any programming language is to make efficient use of memory. As applications grow, it's very common for Stringliterals to occupy large area of memory, which can even cause redundancy. So, in order to make Java more efficient, the JVM sets aside a special area of memory called the "String constant pool". When the compiler sees a Stringliteral, it looks for the Stringin the pool. If a match is found, the reference to the new literal is directed to the existing Stringand no new Stringobject is created. The existing Stringsimply has one more reference. Here comes the point of making Stringobjects immutable: In the Stringconstant pool, a Stringobject is likely to have one or many references. If several references point to same Stringwithout even knowing it, it would be bad if one of the references modified that Stringvalue. That's why Stringobjects are immutable. Well, now you could say, what if someone overrides the functionality of Stringclass? That's the reason that the Stringclass is marked finalso that nobody can override the behavior of its methods.
| |||||||||||||||||||||
|
相关文章推荐
- What is a JavaBean exactly?
- What exactly is copy-on-modify semantics in R, and where is the canonical source?
- CATALINA_OPTS v JAVA_OPTS - What is the difference?
- Why String is immutable or final in Java
- Is the Improvement of String.substring() in Java7 Really Reasonable?
- What Exactly Is the Error?
- 为什么Java中的String是不可变的?(Why String is immutable o...
- The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
- Why String is immutable in Java ?--reference
- What is the difference between a soft reference and a weak reference in Java?
- What is the ARGB int encoding of pixels in Java's AWT?
- Constraint Layout [Animations | Dynamic Constraints | UI by Java] ( What the hell is this )[Part3]
- What is the C# equivalent to C++ OutputDebugString?
- What exactly is the difference between WndProc and DefaultWndProc?
- Eclipse导入工程出错The type java.lang.String cannot be resolved. It is indirectly referenced from required
- What Is The Meaning of 0.0.0.0 In Routing Table?
- 问题1-The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
- What is the probability that you hire exactly twice?
- Why String is Immutable or Final in Java
- What exactly is the difference between “pass by reference” in C and in C++?