You are not moving heap data to the String constants pool when you call intern
, you are just adding another new String to the constants pool if it doesn't already exist in the constants pool (which doesn't happen in your case as "Test"
is already added to constants pool in line -1).
You might want to change your code to :
public static void main(String[] args) {
String s3 = new String("Test");
s3 = s3.intern();
String s4 = "Test";
System.out.println(s3 == s4);//fasle(i need true)
}
In the above code you assign the reference to the interned value of s3 to s3 again. Next,you get the same object from String constants pool in S4.