Open In App

Storage of String in Java

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, we know both Stack and Heap space are part of Java Virtual Machine (JVM). However, these memory spaces are used for different purposes. Stack space contains specific values that are short-lived whereas Heap space is by Java Runtime to allocate memory to objects and JRE classes. In Java, strings are stored in the heap area. 

Prerequisite:  Strings in Java

Why are Java strings stored in Heap, not in Stack? 

Well, String is a class, and strings in Java are treated as an object, hence the object of String class will be stored in Heap, not in the stack area. Let’s go deep into the topic. As we all know we can create string objects in two ways i.e 

  1. By string literal 
  2. By using the ‘new’ keyword 

String Literal is created by using a double quote.

Example: String s=Welcome; 

Here the JVM checks the String Constant Pool. If the string does not exist then a new string instance is created and placed in the pool if the same string exists then it will not create a new object rather it will return the reference to the same instance. So java compiler does not allocate new memory for literals if they have the same string. The cache that stores these string instances is known as String Constant pool or String Pool. In earlier versions of Java up to JDK 6 String pool was located inside PermGen(Permanent Generation) space. But in JDK 7 it is moved to the main heap area.

But if we create an object using a new keyword then it creates a new reference of the object.

Why did the String pool move from PermGen to the normal heap area?

PermGen space is limited space, the default size is just 64 MB. And it was a problem of creating and storing too many string objects in PermGen space. That’s why the String pool is moved to a larger heap area. To make the java more memory efficient the concept of string literal is used.

By the use of ‘new’ keyword, the JVM will create a new string object in the normal heap area even if the same string object present in the string pool. 

For Example:

String a=new String(“Trident”);

Let’s have a look to the concept with a Java program and visualize the actual JVM memory Structure:

Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
       
          // String created using String literal
        String s1 = "TAT";
        String s2 = "TAT";
       
          // String created using 'new' keyword
        String s3 = new String("TAT");
        String s4 = new String("TAT");
       
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }
}


 

Output:

TAT
TAT
TAT
TAT

The below figure illustrates the storage of String :

Storage of String in Java

Here in the above figure, the String is stored in String constant pool. String object reference variables are stored in the stack area under the method main( ). As main( ) is having some space in the stack area.

Note:

  • All objects in Java are stored in the heap. The reference variable to the object stored in the stack area or they can be contained in other objects which puts them in the heap area also.
  • The string is passed by reference by java.
  • A string reference variable is not a reference itself. It is a variable that stores a reference (memory address).


    Like Article
    Suggest improvement
    Previous
    Next
    Share your thoughts in the comments

    Similar Reads