0% found this document useful (0 votes)
39 views

String in Depth

1) When using the new operator, a new String object is always created in the heap even if another identical String exists in the heap or string constant pool (SCP). Duplicate objects are allowed in the heap but not in SCP. 2) The == operator checks if two references refer to the same object. The equals() method checks if the objects have the same value. 3) The String class is immutable, so any operation that would modify a String actually returns a new String object. This new object is created in the heap to allow for runtime modifications.

Uploaded by

Pourush Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

String in Depth

1) When using the new operator, a new String object is always created in the heap even if another identical String exists in the heap or string constant pool (SCP). Duplicate objects are allowed in the heap but not in SCP. 2) The == operator checks if two references refer to the same object. The equals() method checks if the objects have the same value. 3) The String class is immutable, so any operation that would modify a String actually returns a new String object. This new object is created in the heap to allow for runtime modifications.

Uploaded by

Pourush Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

String

3 concept
1st
Difference b/w

String s1 = new String("manshu");


String s2 = "manshu";
String object

String s1= new String ("manshu");

Heap SCP
In this case two objects will be created one is in the
heap
s1-->manshu manshu
other one is in SCP(String constant pool) and

s1 is always pointing to heap object.


String literal

String s2 = "manshu";
Heap SCP

In this case only one object


will be created in SCP s2-> manshu
and
s is always
referring that object.

most imp difference


conclusion
Whenever we are using new operator compulsory a
new object will be created on the
Heap .

In heap in SCP
There may be a chance of existing two objects with
Duplicates are Duplicate
s are
same content on the heap allowed not allow
ed

but

there is no chance of existing two objects with same


content on SCP .
Goals

Heap
String s1= new String ("manshu");
String s2 = new String ("manshu"); SCP
String s3 = new String ("manshu"); s1-->manshu
String s4 = new String ("manshu"); s2-->manshu
s3-->manshu manshu
String s5 = new String ("manshu");
s4-->manshu
s5-->manshu
Goals conclusion

String s1 = new String("shivam"); In heap



in SCP
String s2 = new String ("shivam");

String s3 = "shivam"; s1--> shivam

shivam
s2--> shivam
String s4 = "shivam";
s3
s4
2nd

== vs equal()
in String class
String s1 = new String("manshu");
String s2 = new String("manshu");
Heap
String s3 = new String("shivam");

SCP

String s4 = "manshu";

s1-->manshu s4->

manshu
String s5 = "manshu"; s2-->manshu
s3 --> shivam
s5->

s6-> shivam
String s6 = "shivam"

String s1 = new String("manshu");


String s2 = new String("manshu");

no
s1 == s2 ? because both are in heap and
refering to different object Heap
SCP

s1-->manshu s4->
s5-> manshu
s2-->manshu

yes s3 --> shivam


s6-> shivam
s1.equal(s2) because both content are same

String s1 = new String("manshu");


String s4 = "manshu";"

no
because s1 is in heap pointing to
s1 == s4 ? different than that of s4 which is
Heap

SCP

in scp s1-->manshu s4->


s5-> manshu
s2-->manshu

s3 --> shivam
s6-> shivam

s1.equal(s4) yes
because both content are same
String s4 = "manshu";
String s5 = "manshu";

Heap
yes

SCP

because both s4 and s5 are in

s4 == s5 ? SCP and in SCP duplicate are s1-->manshu


s2-->manshu
s4->
s5-> manshu

not allowed thus both are s3 --> shivam


s6-> shivam
pointing to same object

3rd

Immutability
String can not change

same like

what you have written with


pen that can't be change
Once we create a String object we can't perform any
changes in the existing object.

If we are try to perform any changes


with those changes
a new object will be created.

This behavior is called


immutability of the String object.
For every String Constant one object will be
created in SCP.

Because of runtime
operation if an object is required to create
compulsory that object should be placed on
the heap but not SCP.
String s=new String("manshu");
s.concat(" shivam");
System.out.println(s);//manshu

Heap

SCP

s-->manshu manshu

(no reference) manshu shivam shivam


String s= "manshu";
s.concat(" shivam");
System.out.println(s);//manshu

Heap SCP


s -> manshu
(no reference) manshu shivam


shivam
String s1 = new String ("manshu")
String s2 = s1.toUpperCase();
String s3= s1.toLowerCase();

System.out.println(s1 == s2);
System.out.println( s1 ==s3);

what will be output ?? -->


Note -

most valuable concept Because runtime operation

ifthere is a change in content


with those changes a new object will be created only
on the heap but not in SCP.

If there is no change in wrt current content(object)


no new object will be created
the same object will be reused.
This rule is same whether object present on the Heap
or SCP
String s1 = new String ("manshu")
String s2 = s1.toUpperCase();
String s3= s1.toLowerCase();

System.out.println(s1 == s2); // false


System.out.println( s1 ==s3); // true but why ?
solution
Heap


SCP
s1 --> manshu

(no reference)
manshu
s2 -->MANSHU

String s3 = s1.toLowerCase(); --> wtr s1, s3 content is same


Heap

SCP

s1 --> manshu
s3-->
manshu

s2 -->MANSHU

String s1 = new String ("manshu")


String s2 = s1.toUpperCase();

String s4 = s2.toLowerCase();

;
System.out.println( s1==s4); //false why ?
solution
Heap


SCP
s1 --> manshu

(no reference)
manshu
s2 -->MANSHU

String s4 = s2toLowerCase(); --> wtr s2, s4 has different content


Heap


SCP
s1 --> manshu


manshu

s2 -->MANSHU

s4 --> manshu

Follow for such content


disclaimer :-
this slide does not make any
sence here

in Hindi String is called "dhaga"

and Kabir explains immutability concept like

रहिमन धागा प्रेम का, मत तोड़ो चटकाय।


टूटे से फिर ना जुड़े, जुड़े गाँठ परि जाय॥

string can not change


if you change new object will be created

You might also like