
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differences Between Heap Memory and String Constant Pool in Java
The Heap Memory and String Constant Pool are two different memory locations in Java where objects are stored during the execution of programs. Both memory areas are managed by the Java Virtual Machine.
In this article, we are going to discuss the difference between Heap Memory and String Constant Pool in Java.
Heap Memory
Heap memory is a runtime data area from which memory for all class instances and arrays is allocated. It is the main memory area used for dynamic memory allocation in Java.
Example
Creating strings with the new keyword always allocates new memory in the heap. In this example, the str1 refers to a new object in the heap. And, the str2 also refers to another new object in the heap, even though the text is the same.
public class Example { public static void main(String[] args) { String str1 = new String("Tutorialspoint"); String str2 = new String("Tutorialspoint"); System.out.print("Is both strings equal: "); System.out.println(str1 == str2); } }
The above Java program will produce the following output:
Is both strings equal: false
String Constant Pool
Within the heap, the String constant pool is located. It stores unique string literals. Whenever a new string variable is created, the JVM will match the specified value with the values within the pool.
In case of a match, instead of creating a new string (new memory location) JVM will use the existing value.
Example
String literals with the same value share the same memory reference in the String Constant Pool. In the following example, both str1 and str2 are assigned the same string literal. Hence, the JVM will reuse the reference instead of creating a new one.
public class Example { public static void main(String[] args) { String str1 = "Tutorialspoint"; String str2 = "Tutorialspoint"; System.out.print("Is both strings equal: "); System.out.println(str1 == str2); } }
When you run the above code, it will give the following result:
Is both strings equal: true
Heap Memory VS String Constant Pool
In the table given below, the difference between heap memory and string constant pool is listed:
Heap Memory |
String Constant Pool |
---|---|
The heap memory is a run-time data area from which the memory for all Java class instances and arrays is allocated. |
String uses a special memory location to reuse String objects called the String Constant Pool. |
The heap is created when the JVM starts up and may increase or decrease in size while the application runs. |
String objects created without the use of the new keyword are stored in the String Constant Pool, which is a part of the heap. |
The size of the heap can be specified using the -Xms VM option. The heap can be of fixed size or variable size, depending on the garbage collection strategy. The maximum heap size can be set using the-Xmx option. |
The String Constant Pool is automatically managed by the JVM and does not require manual size configuration. |
The heap is used for all kinds of objects, including arrays and custom classes. |
Only string literals are stored in the String Constant Pool. |