Today i found out that string declared with "" are placed in string constant pool while with new keyword its in heap. String.intern() return string if defined in string pool otherwise creates the string and then returns it.
Transcribed from the original LinkedIn image post.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| public class Day07 {
public static void main(String args[]) {
// String is created in heap
String file1 = new String("docx");
String file2 = new String("docx");
// checking for memory location
System.out.println(file1 == file2); // false
// String is created in String constant pool
String file3 = "rs";
String file4 = "rs";
System.out.println(file3 == file4); // true
// now using intern() getting a copy of docx in String pool
String file5 = file2.intern();
String file6 = "docx";
// first check file2 and file5
System.out.println(file2 == file5);
// next check file5 and file6
System.out.println(file5 == file6);
}
}
|
The original LinkedIn graphic is preserved below.
