Storage of String in Java
Last Updated :
08 Jan, 2024
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
- By string literal
- 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 :

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).
Similar Reads
StringJoiner toString() method in Java The toString() of StringJoiner is used to convert StringJoiner to String. It returns the current value, consisting of the prefix, the values added so far separated by the delimiter, and the suffix, unless no elements have been added in which case, the prefix + suffix or the emptyValue characters are
2 min read
Object toString() Method in Java Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth, it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object, and if it extends another class, then it is
3 min read
String Literal Vs String Object in Java Understanding the difference between String Literals and String Objects in Java plays a very important role. The main difference between String Literal and String Object is listed below:String Literal: A sequence of characters inside double quotes is known as a String Literal. String Literals are st
3 min read
Interning of String in Java String Interning in Java is a process of storing only one copy of each distinct String value, which must be immutable. Applying String.intern() on a couple of strings will ensure that all strings having the same contents that shares the same memory.Example:When a string is created using a string lit
4 min read
String Constant Pool in Java In Java, Strings are the type of objects that can store the character of values. A string acts the same as an array of characters in Java. Memory allocation is not possible without String Constant Pool. In this article, we will learn about Java String Constant Pool.What is Java String Pool?A Java St
5 min read
String Class in Java A string is a sequence of characters. In Java, objects of the String class are immutable, which means they cannot be changed once created. In this article, we are going to learn about the String class in Java.Example of String Class in Java:Java// Java Program to Create a String import java.io.*; cl
7 min read
Java.lang.String class in Java | Set 2 Java.lang.String class in Java | Set 1 In this article we would be discussing different constructor and methods provided by java.lang.String. Strings in java are immutable. Now lets discuss some of the methods provided by String class. Methods: public int codePointAt(int index) - It takes as paramet
5 min read
String Arrays in Java A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.When we create an array of type String in Java, it is called a String Array
5 min read
Output of Java Programs | Set 52 (Strings Class) Prerequisite : Basics of Strings class in java 1. What is the Output Of the following Program Java class demo1 { public static void main(String args[]) { String str1 = "java"; char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' }; String str2 = new
5 min read