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 GSK

Hi, i am Santosh Gadagamma, a tutor in Software Engineering and an enthusiast for sharing knowledge in Computer Science and other domains. I developed this site to share knowledge to all the aspirants of technologies like, Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts and all levels of software project design, development, deployment, and maintenance. As a programmer I believe that, "The world now needs computers to function." Hope, this site guides you as a learning tool towards greater heights. I believe that Education has no end points and i wish to learn more in the process of teaching you.

Check Also

What is block chain technology?

What is Block chain Technology? Block chain technology is technology word which heard frequently in …