---
title: "What is string pool in java"  
description: "What is string pool in java"  
author: "Anonymous User"  
published: 2015-07-10  
updated: 2020-09-23  
canonical: https://www.mindstick.com/interview/2693/what-is-string-pool-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# What is string pool in java

String Pool is a pool of strings stored in Java heap memory. String objects can be created either by new operator or by specifying the values in double quotes.\
**Method 1:** When a new string is created using double quotes, JVM searches string pool for the string with the same value. if it finds a string which matches the values, it will return the reference of the string. Else it will create a new string in the pool and returns that reference.

```
String s1 = "Cat"; String s2 = "Cat"; if(s1 == s2) System.out.println("equal"); //Prints equal.
```

\
**Method 2:**When new operator is used to create a string, String class will be forced to create a new String object. To put the newly created string into the pool or assign it to another string, use intern().

```
String n1 = new String("ABCD"); String n2 = new String("ABCD"); if(n1 == n2) System.out.println("equal"); //No output.
```

## Answers

### Answer by Anonymous User

String Pool is a pool of strings stored in Java heap memory. String objects can be created either by new operator or by specifying the values in double quotes.\
**Method 1:** When a new string is created using double quotes, JVM searches string pool for the string with the same value. if it finds a string which matches the values, it will return the reference of the string. Else it will create a new string in the pool and returns that reference.

```
String s1 = "Cat"; String s2 = "Cat"; if(s1 == s2) System.out.println("equal"); //Prints equal.
```

\
**Method 2:**When new operator is used to create a string, String class will be forced to create a new String object. To put the newly created string into the pool or assign it to another string, use intern().

```
String n1 = new String("ABCD"); String n2 = new String("ABCD"); if(n1 == n2) System.out.println("equal"); //No output.
```


---

Original Source: https://www.mindstick.com/interview/2693/what-is-string-pool-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
