What is mean by immutable object/class?
Assume we have a below class A as mutable:
Class A{
int i = 10;
int j = 20;
A a = new A();
}
a.i = 30;
a.j = 40;
Changing state of objects, so A is mutable.
Immutable: once object is created, we cannot change the content of the object.
String str = "Venu" -> We ca not change the state of str because string is immutable
str.concate("gopal) -> Creates a new object with modified content
s.o.p (str) = Venu
If asign ablove concatenate operation to a string s and print s then
String s = str.concate("gopal) ==> Venugopal
Why String is immutable in Java?
What is SCP:
The Java string constant pool is an area in heap memory where Java stores literal string values. The heap is an area of memory used for run-time operations.
String str = "Venu"
Class Student{
String str = "Venu"
String str1 = "Venu"
String str2 = "Venu"
Str2.concate("gopal")
}
line1: Whenever create using string leteral, an Object is created inside SCP with the contant "Venu"
line2: Creating same as string literal, which is already available with the same contant in SCP so a new reference is point out to existing the object.
(Duplicates are not allowed in SCP)
line3: Same happens as happened in line 2.
How SCP helps for memory optimization:
Suppose we have 10,000 students with same name, 10,000 objects will be created inside the memory, performance will be reduced.
We should not create a more objects, SCP has been implemented inside JVM it will create once object referece to same object if trying to create with the same name.
Str2.concate("gopal") -> instead it point of str2, creates new object in Heap area as we are going some runtime operation. (Note: str, str1 also get effected if strings are mutable)
String s1 = new String("Venu") -> new keyword always creates a new object in Heap memory.
But, if we want to restrict to create a new object and retrieving same object which is created with the String literal then we can use intern() method.
if(str == s1){
True;
}
String intern():
is method in String class. It returns a canonical representation for the string object.
When the intern() method is invoked on a String object it looks the string contained by this String object in the pool, if the string is found there then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
Note: Java automatically interns all Strings by default.
Assume we have a below class A as mutable:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2FT9ahLRfCoGU4CAl9OZa4et-kilrW1xpF-dQBlrhv_dh_FS53muZfRzy75Z4QDrXjv11cyEjST8NbFs9rAmNQftjIxszkhGqFvhtG5FfYlYTBPQJwxwRKQQT80ouOTKhho2FMhlVABc/s320/SCP.png)
int i = 10;
int j = 20;
A a = new A();
}
a.i = 30;
a.j = 40;
Changing state of objects, so A is mutable.
Immutable: once object is created, we cannot change the content of the object.
String str = "Venu" -> We ca not change the state of str because string is immutable
str.concate("gopal) -> Creates a new object with modified content
s.o.p (str) = Venu
If asign ablove concatenate operation to a string s and print s then
String s = str.concate("gopal) ==> Venugopal
Why String is immutable in Java?
What is SCP:
The Java string constant pool is an area in heap memory where Java stores literal string values. The heap is an area of memory used for run-time operations.
String str = "Venu"
Class Student{
String str = "Venu"
String str1 = "Venu"
String str2 = "Venu"
Str2.concate("gopal")
}
line1: Whenever create using string leteral, an Object is created inside SCP with the contant "Venu"
line2: Creating same as string literal, which is already available with the same contant in SCP so a new reference is point out to existing the object.
(Duplicates are not allowed in SCP)
line3: Same happens as happened in line 2.
How SCP helps for memory optimization:
Suppose we have 10,000 students with same name, 10,000 objects will be created inside the memory, performance will be reduced.
We should not create a more objects, SCP has been implemented inside JVM it will create once object referece to same object if trying to create with the same name.
Str2.concate("gopal") -> instead it point of str2, creates new object in Heap area as we are going some runtime operation. (Note: str, str1 also get effected if strings are mutable)
String s1 = new String("Venu") -> new keyword always creates a new object in Heap memory.
But, if we want to restrict to create a new object and retrieving same object which is created with the String literal then we can use intern() method.
if(str == s1){
True;
}
String intern():
is method in String class. It returns a canonical representation for the string object.
When the intern() method is invoked on a String object it looks the string contained by this String object in the pool, if the string is found there then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
Note: Java automatically interns all Strings by default.