Before proceeding further with the fuss of immutability, let’s just take a look into the
String class and its functionality a little before coming to any conclusion.
This is how
String str = "knowledge";
This, as usual, creates a string containing
"knowledge" and assigns it a reference
str. Simple enough? Lets perform some more functions:
// assigns a new reference to the // same string "knowledge" String s = str;
Let’s see how the below statement works:
str = str.concat(" base");
This appends a string
" base" to
str. But wait, how is this possible, since
String objects 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
String object, gives it a value
"knowledge base", and gives it a reference
An important point to note here is that, while the
String object 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
At this point in the example above, we have two
String objects: 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
String objects, the third one being the literal
"base" in the
Important Facts about String and Memory usage
What if we didn’t have another reference
"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
s1 refers to java
- The first line is pretty straightforward: create a new
- Next, the VM creates another new
"java rules", but nothing refers to it. So, the second
Stringis instantly lost. We can’t reach it.
The reference variable
s1 still refers to the original
Almost every method, applied to a
String object in order to modify it, creates new
String object. So, where do these
String objects 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
String literals 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
String literal, it looks for the
String in the pool. If a match is found, the reference to the new literal is directed to the existing
String and no new
String object is created. The existing
String simply has one more reference. Here comes the point of making
String objects immutable:
String constant pool, a
String object is likely to have one or many references. If several references point to same
String without even knowing it, it would be bad if one of the references modified that
String value. That’s why
String objects are immutable.
Well, now you could say, what if someone overrides the functionality of
String class? That’s the reason that the
String class is marked
final so that nobody can override the behavior of its methods.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Primitive Wrapper Classes are Immutable in Java
- How to create Immutable class in Java?
- Factory method to create Immutable List in Java SE 9
- Factory method to create Immutable Set in Java 9
- Factory method to create immutable Map in Java 9
- Immutable List in Java
- Immutable Map in Java
- Immutable Set in Java
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Construct a string that has exactly K subsequences from given string
- Smallest String consisting of a String S exactly K times as a Substring
- String which when repeated exactly K times gives a permutation of S
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Check if the string contains consecutive letters and each letter occurs exactly once
- Find if string is K-Palindrome or not using all characters exactly once
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Remove characters from a String that appears exactly K times
- Find the String having each substring with exactly K distinct characters
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.