0% found this document useful (0 votes)
12 views

String Constant Pool Breakdown for SDETs in JAVA

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

String Constant Pool Breakdown for SDETs in JAVA

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

01/05

String Constant
Pool in JAVA
Breakdown
LinkedIn: Japneet Sachdeva

SWIPE
02/05

String Constant Pool (SCP)


String pool is nothing but a storage area in JAVA
heap where string literals stores. When we create a
string literal, the JVM first check that literal in the
String pool.

If the literal is already present in the pool, it returns a


reference to the pooled instance. If the literal is not
present in the pool, a new String object takes place
in the String pool

LinkedIn: Japneet Sachdeva

KEEP SWIPING
03/05

String Literal
String str1 = "Python";
String str2 = "Data Analyst";
String str3 = "Python";

Using new Keyword


In Java, a new keyword is also used to create String, as follows:

String str4 = new String ("Java");


String str5 = new String ("C++");
String str6 = new String ("Data Analyst");

Let's understand what is the difference between them. Let's


compare the string literals' references.

s1==s3 //true
s2==s3 //false LinkedIn: Japneet Sachdeva

KEEP SWIPING
03/05
JAVA Heap

SCP
str1 Python
str2 Data Analyst
str3

str4 “JAVA”
str5 “C++”
str6 “Data Analyst”

LinkedIn: Japneet Sachdeva

ONE MORE
04/05

Explanation
First, we have created a string literal “Python” and it takes place
in the pool. After that, the string “Data Analyst” is created, it also
takes place in the pool.

At last, again we have created the string Python. But at this


time, JVM checks for the string and found that string literal is
already present. Instead of taking a new instance in the String
pool, it returns the reference instance. Meaning of “str1”

Similarly,

When we create String vars using new keyword then for those,
memory is allocated in heap rather then in SCP.

LinkedIn: Japneet Sachdeva

ONE MORE
05/05

Do follow for more!

LinkedIn: @Japneet Sachdeva

You might also like