-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternMethodDemo.java
More file actions
206 lines (116 loc) · 4.12 KB
/
Copy pathInternMethodDemo.java
File metadata and controls
206 lines (116 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*Program 03 : Demonstrate the `intern()` Method
## Problem Statement
Write a Java program to demonstrate how the `intern()` method works and how it returns a reference from the String Pool.
/*
------------------------------------------------------------
Program 03 : Demonstrate the intern() Method
Language : Java
Difficulty : ⭐⭐ Beginner to Intermediate
Concepts Covered
✔ String Pool
✔ intern()
✔ Heap Memory
✔ == Operator
Expected Time : 15 Minutes
------------------------------------------------------------
*/
public class InternMethodDemo {
public static void main(String[] args) {
String first = new String("Java");
String second = first.intern();
String third = "Java";
System.out.println("first == second : " + (first == second));
System.out.println("second == third : " + (second == third));
System.out.println("first.equals(third) : " + first.equals(third));
}
}
/*
------------------------------------------------------------
Output
first == second : false
second == third : true
first.equals(third) : true
------------------------------------------------------------
Explanation
First, a new String object is created using the new keyword.
String first = new String("Java");
This object is stored in Heap Memory.
Next,
String second = first.intern();
The intern() method checks whether an identical String already exists in the String Pool.
If it exists, it returns the reference of the pooled String.
If it does not exist, Java adds the String to the String Pool and returns its reference.
Finally,
String third = "Java";
Since "Java" already exists in the String Pool,
third refers to the pooled object.
Therefore,
first == second
returns false because first points to the Heap object while second points to the String Pool object.
second == third
returns true because both variables point to the same object in the String Pool.
first.equals(third)
returns true because equals() compares the contents of the Strings.
------------------------------------------------------------
Interview Notes
The intern() method is used to obtain the String Pool reference of a String.
Method Signature
public native String intern();
Remember:
new String("Java")
creates a Heap object.
intern()
returns the String Pool reference.
------------------------------------------------------------
Important Points
✔ intern() returns a reference from the String Pool.
✔ It does not create another Heap object.
✔ intern() is useful when many duplicate Strings exist and memory optimization is required.
✔ == compares references.
✔ equals() compares values.
------------------------------------------------------------
Common Mistakes
❌ Thinking intern() creates another object.
❌ Assuming intern() returns the Heap object.
❌ Using == before understanding String Pool.
------------------------------------------------------------
Top Company Interview Focus
★★★★★ Oracle
★★★★★ Amazon
★★★★★ Microsoft
★★★★★ Google
★★★★★ Adobe
★★★★☆ Atlassian
★★★★☆ Walmart Global Tech
This is a very common JVM interview topic.
------------------------------------------------------------
Follow-up Interview Questions
1. What does intern() return?
2. Why is intern() used?
3. Does intern() create a new object?
4. Where is the returned object stored?
5. Difference between Heap Memory and String Pool?
------------------------------------------------------------
Real World Use Cases
✔ Memory optimization
✔ JVM internals
✔ High-performance applications
✔ Applications processing millions of duplicate Strings
------------------------------------------------------------
Practice Questions
1.
String s1 = new String("Java");
String s2 = s1.intern();
Predict the output of
s1 == s2
----------------------------
2.
String s1 = "Java";
String s2 = new String("Java").intern();
Predict the output of
s1 == s2
----------------------------
3.
Explain why intern() can reduce memory usage.
------------------------------------------------------------
*/