Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

JAVA

Concepts regarding memory allotment to String


objects
String is an immutable class in java.
Strings are represented via objects in java.
We can create a String object in java by two ways1) String Literal :
String s=HelloWorld;
2) new keyword :
String s=new String(HelloWorld);
But there is a difference in memory allotment to both objects by
JVM.Lets us understand each of them one by one1)Using String Literals
->Whenever a String object is made via String literal then the
object is allotted memory in special memory area called String
Constant Pool(SCP) rather than heap area.
For exampleString obj1=Hi;

obj1

obj2

Hi

World

String obj2=World;
Reference ID of object Hi goes to obj1 .
Reference ID of object World goes to obj2 .

String Constant
Pool(SCP)

->String Constant Pool(SCP) does not contain duplicate objects of


String class.As String objects are immutable ,they can be shared.
Understand this with an example-

Suppose we create two String objects like thisString obj1-Hi;

//line 1

String obj2=Hi;

//line 2

Hi
obj1

Hi

Reference ID of obj1 and obj2 is shared.

String Constant

Pool(SCP)
When JVM executes line 1,it creates a String
object in SCP as it is
a String literal and the reference ID of this object is kept in
obj1.Now when JVM executes line 2 it encounters that the content
in obj2 i.e.Hi , is same as in obj1 thus the JVM does not create
another object in SCP but it assigns the same reference ID to obj2.

You can counter check it byIf(obj1==obj2) { System.out.println(same reference id); }


Note-In String Literals, if the content of objects is different ,then a
new object is created but if it is same then no new object is
created.
->Garbage Collection is not done in String class objects of String
Constant Pool.
2) Using new keyword

->Whenever a String object is created via new keyword then


always a new memory is allocated to every object in heap area.
For example-

Obj11

Hi

String obj1=new String(Hi);


String obj2=new String(World);
Reference ID of Hi goes to obj1.

Obj2

World
Heap Area

Reference ID of World goes to ob2.

->In creating object via new keyword JVM does not check for
duplicity and create a new object everytime.

For exampleFor example-

Obj11

Hi

String obj1=new String(Hi);


String obj2=new String(Hi);

Obj2

Hi
Heap Area

Reference ID is not shared.

->Garbage collection can be done in String objects in Heap area.


-----------------------------------------------Deep Analysis regarding memory

allocation------------------->To identify which object is created from new and which object
is created from literal JVM kept them in seprate areas i.e. heap
area and SCP respectively.
->Now try to understand this important conceptString obj=new String(HelloWorld);
When we create object via new by passing a String literal in
constructor,then actually two objects are created
1)String literal i.e. HelloWorldwhich is passed in constructor is
first created in SCP.
2)Object formed by new in heap area whose reference ID is in
obj.
For exampleEvaluate the code in this orderString r1=new String(Hi);
String r2=r1.intern();
String r3=new String(Hi);

String r4=r3.intern();

String r1=new String(Hi);


Hi
Hi
Heap Area

Hi
SCP

String r2=r1.intern();

String r4=r3.intern();
String r3=new String(Hi);

Note: intern function returns the reference Id of same string

from SCP area.

Here r2 and r4 are same but r1 and r3 are not same.

You might also like