Memory Allotment of String

Whenever a String Object is created as a literal, the object will be created in the String constant pool. This allows JVM to optimize the initialization of String literal.

Example: 

String demoString = "Geeks";

The string can also be declared using a new operator i.e. dynamically allocated. In case of String are dynamically allocated they are assigned a new memory location in the heap. This string will not be added to the String constant pool.

Example: 

String demoString = new String("Geeks");

If you want to store this string in the constant pool then you will need to “intern” it.

Example:

String internedString = demoString.intern(); 
// this will add the string to string constant pool.

It is preferred to use String literals as it allows JVM to optimize memory allocation.

An example that shows how to declare a String 

Java
// Java code to illustrate String
import java.io.*;
import java.lang.*;

class Test {
    public static void main(String[] args)
    {
        // Declare String without using new operator
        String name = "w3wiki";

        // Prints the String.
        System.out.println("String name = " + name);

        // Declare String using new operator
        String newString = new String("w3wiki");

        // Prints the String.
        System.out.println("String newString = " + newString);
    }
}

Output
String name = w3wiki
String newString = w3wiki


Note: String Object is created in Heap area and Literals are stored in special memory area known as string constant pool.

Strings in Java

In the given example only one object will be created. Firstly JVM will not find any string object with the value “Welcome” in the string constant pool, so it will create a new object. After that it will find the string with the value “Welcome” in the pool, it will not create a new object but will return the reference to the same instance. In this article, we will learn about Java Strings.

Similar Reads

What are Strings in Java?

Strings are the type of objects that can store the character of values and in Java, every character is stored in 16 bits i,e using UTF 16-bit encoding. A string acts the same as an array of characters in Java....

Ways of Creating a String

There are two ways to create a string in Java:...

Interfaces and Classes in Strings in Java

CharBuffer: This class implements the CharSequence interface. This class is used to allow character buffers to be used in place of CharSequences. An example of such usage is the regular-expression package java.util.regex....

Immutable String in Java

In Java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once a string object is created its data or state can’t be changed but a new string object is created....

Memory Allotment of String

Whenever a String Object is created as a literal, the object will be created in the String constant pool. This allows JVM to optimize the initialization of String literal....

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

PermGen space is limited, the default size is just 64 MB. it was a problem with creating and storing too many string objects in PermGen space. That’s why the String pool was moved to a larger heap area. To make Java more memory efficient, the concept of string literal is used. By the use of the ‘new’ keyword, The JVM will create a new string object in the normal heap area even if the same string object is present in the string pool....

Frequently Asked Questions

1. What are strings in Java?...