« Back
package com.string; public class StringIntern { public static void main(String[] args) { // S1 refers to Object in the Heap Area String str1 = new String("coders"); // Line-1 // S3 refers to Object in the SCP Area String str3 = "coders"; // Line-3 // S2 refers to Object in SCP Area String str2 = str1.intern(); // Line-2 // Comparing memory locations // s1 is in Heap // s2 is in SCP System.out.println(str1 == str2); // Comparing only values System.out.println(str1.equals(str2)); // comparing memory locations in SCP //str2 now is having location of str1 from StringPoolConstant because when str2 = str1.intern() // it will return the string copy from the pool System.out.println(str2 == str3); str4= str3.intern(); System.out.println(str4 == str3); } }
false
true
true
true
×
Report a Problem:
Description:
Submit