Home / Posts / Why String Immutable
Heap String
Heap Memory

Why String Immutable

Before going into the discussion first need to know about the mutable and immutable. Mutable means it can be modified immutable means it cannot be modified once it is created.
Strings in Java are immutable i.e. once the string is created it cannot be modified. More over a string in java is final. As we are all know about final, it is a key word which can be used for constant and cannot be sub classed or never changed.
How the strings are created in Memory:
Creating a string is done by “new” key word. It creates an object in memory. Java has special functionality to storing these string objects. The Java Virtual Machine split the memory into two parts stack and heap. Heap memory is used for storing the variable and other storage purpose and stack is used for program execution purpose.
While executing the program java creates string pool in Heap memory. This memory is especially for string literals. Whenever we create an object for string that object stored in heap memory and controlled by heap. String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String.
When the user creates a string by using quotes “”it looks for the String with same value in the String pool, if it found return the reference else creates a new Sting in the pool and return the reference.

String Pool demo Program:

String Pool demo:

public class StringPoolDemo {

public static void main(String[] args) {

String s1 = “hai”;

String s2 = “java”;

String s3 = new String(“hai”);

if(s1==s2){

System.out.println(“s1 == s2 both are same and return true”);

}else{

System.out.println(“not equal”);

}

System.out.println(“s1 == s3 :”+(s1==s3));

}

}

Output of the above program is:

Not equal

S1 == s3 : false

Why s1 == s3 prints false

In the s1 already there exists a string a literal so it could not create another literal with same name hai.
The hash code of the sting is used in java for strings in java for exact location of string.
See the below picture for String Pool in Heap Memory.
String s1 = “hai”;

String s2 = “java”;

String s3 = “123”;

String s4 = “abcd”;

Here S1, S2,S3 and S4 are string objects.

Heap String
Heap Memory

About Santosh Kumar Gadagamma

I'm Santosh Gadagamma, an Experienced Software Engineer passionate about sharing knowledge in technologies like Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts, and all levels of software design, development, deployment, and maintenance. I believe computers are essential for the world's functioning, and I'm committed to helping others learn the skills they need to succeed in tech. My website is a valuable learning tool to help you reach greater heights in your education and career, and I believe that education has no end points.

Check Also

Google Keyword Planner

How to Use Google Key Word Planner

Google Keyword Planner is a free tool provided by Google Ads to help businesses and …