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
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read