HTML Java

Java String Constant Pool


String Constant Pool

JVM introduces special memory area for Strings called String Constatnt Pool whenever we cretaes String literal then JVM first check in the pool and if the literal is present then it return the refernce of the String which is present in the pool or else created new String.

Example

package com.string;

public class JavaStringPool {

public static void main(String[] args) {
String s1 = "coders";
String s2 = "arts";
String s3 = "coders";
if (s1==s3) {
System.out.println("Both string refer to the same object");
}

//different reference
if (s1==s2) {
System.out.println("Both strings refer to the same object");
}
else {
System.out.println("Both strings refer to the different object");
}
}

}
Try it »