Thursday, February 28, 2019

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.

0 comments:

Post a Comment

selenium-repo by venu

Blog helps to a student or IT employee to develop or improve skills in Software Testing.
For Online Classes Email us: gadiparthi122@mail.com

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *

Popular Posts