Index Page : Link
Donate : Link
Medium Link : Link
Applications : Link


- SCP stored in method area/PERMGEN till 1.6v, from 1.7v it is stored in Heap

- In above scenario JVM will check that object already exist with this content, if already there then reuse it otherwise create new object.
How many objects will be created?
String s1 = new String("Code Factory");
String s2 = new String("Code Factory");
String s3 = "Code Factory";
String s4 = "Code Factory";

How many objects will be created?
String s = new String("Code");
s.concat("Factory");
s = s.concat("Hello");

How many objects will be created?
String s1 = new String("one");
s1.concat(", two");
String s2 = s1.concat(", three");
s2.concat(", four");
System.out.println(s1); // one
System.out.println(s2); // one, three

How many objects will be created?
String s1 = new String("code factory");
String s2 = new String("code factory");
System.out.println(s1 == s2); // false
String s3 = "code factory";
System.out.println(s1 == s3); // false
String s4 = "code factory";
System.out.println(s3 == s4); // true
String s5 = "code" + " factory";
// s5 will perform at compile time and JVM is responsible
System.out.println(s4 == s5); // true
String s6 = "code";
String s7 = s6 + " factory";
// s7 will perform at runtime
System.out.println(s4 == s7); // false
final String s8 = "code";
// s8 will perform at compile time
String s9 = s8 + " factory";
// s9 will perform at compile time
System.out.println(s4 == s9); // true

- If both are constant then operation is performed at compile time. Ex. s5
- Atleast one normal variable + constant then operation is performed at run time. Ex. s7
- Every final variable will be replaced by compile time. Ex. s8
Importance of String Constant Pool (SCP) :
- Same object can be resused multiple times
- Memory saved
- Performance will be improved

So many String objects are reference to single SCP value (India). So if any one will change there country India to Australia then all objects reference is changed. Thats why immutability concept is come in String.
Move heap area data to String constant pool
To move heap area data to SCP then we have to use intern() method.
public String intern()
- Returns a canonical representation for the string object.
- A pool of strings, initially empty, is maintained privately by the class
String. - When the intern method is invoked, if the pool already contains a string equal to this
Stringobject as determined by theequals(Object)method, then the string from the pool is returned. Otherwise, thisStringobject is added to the pool and a reference to thisStringobject is returned. - It follows that for any two strings
sandt,s.intern() == t.intern()istrueif and only ifs.equals(t)istrue.
package com.example.java.programming;
/**
* @author code.factory
*
*/
public class Test {
public static void main(String... strings) {
String s1 = "Code";
String s2 = new String("Code");
System.out.println(s1 == s2);
s2 = s2.intern();
System.out.println(s1 == s2);
}
}
Output:
false
true
Example 1:
package com.example.java.programming;
/**
* @author code.factory
*
*/
public class Test {
String s1 = "Code";
String s2 = s1;
String s3 = s1;
StringBuilder sb1 = new StringBuilder("Factory");
StringBuilder sb2 = sb1;
StringBuilder sb3 = sb1;
public static void main(String... strings) {
new Test().test();
}
private void test() {
System.out.println(s1 + " : " + s2 + " : " + s3);
s2 = s2.concat("123");
// if we write only s2.concat("123"); then s1 == s2 is true and also s2 will not concated with 123
System.out.println(s1 + " : " + s2 + " : " + s3);
System.out.println("s1 == s2: " + (s1 == s2));
System.out.println("s1 == s3: " + (s1 == s3));
System.out.println();
System.out.println(sb1 + " : " + sb2 + " : " + sb3);
sb2.append("123");
System.out.println(sb1 + " : " + sb2 + " : " + sb3);
System.out.println("sb1 == sb2: " + (sb1 == sb2));
System.out.println("sb1 == sb3: " + (sb1 == sb3));
}
}
Output:
Code : Code : Code
Code : Code123 : Code
s1 == s2: false
s1 == s3: true
Factory : Factory : Factory
Factory123 : Factory123 : Factory123
sb1 == sb2: true
sb1 == sb3: true
Note: Immutability or SCP is required for String because if any one change object’s value then referenced objects will not change.

One thought on “Java – Heap & String Constant Pool (SCP) | Code Factory”