-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompileTimeVsRuntimeConcatenation.java
More file actions
285 lines (158 loc) · 5.27 KB
/
Copy pathCompileTimeVsRuntimeConcatenation.java
File metadata and controls
285 lines (158 loc) · 5.27 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*Program 04 : Compile-Time vs Runtime String Concatenation
## Problem Statement
Write a Java program to demonstrate the difference between **compile-time** and **runtime** String concatenation and how they affect the String Pool.
/*
------------------------------------------------------------
Program 04 : Compile-Time vs Runtime String Concatenation
Language : Java
Difficulty : ⭐⭐⭐ Intermediate
Concepts Covered
✔ String Pool
✔ Compile-Time Concatenation
✔ Runtime Concatenation
✔ == Operator
✔ final Keyword
Expected Time : 15 Minutes
------------------------------------------------------------
*/
public class CompileTimeVsRuntimeConcatenation {
public static void main(String[] args) {
String first = "Hello";
String second = "World";
String compileTime = "Hello" + "World";
String runtime = first + second;
String literal = "HelloWorld";
final String finalFirst = "Hello";
final String finalSecond = "World";
String finalConcatenation = finalFirst + finalSecond;
System.out.println("compileTime == literal : "
+ (compileTime == literal));
System.out.println("runtime == literal : "
+ (runtime == literal));
System.out.println("finalConcatenation == literal : "
+ (finalConcatenation == literal));
}
}
/*
------------------------------------------------------------
Output
compileTime == literal : true
runtime == literal : false
finalConcatenation == literal : true
------------------------------------------------------------
Explanation:
Java performs compile-time optimization whenever all values are known during compilation.
Example:
"Hello" + "World"
is converted by the compiler into
"HelloWorld"
Therefore,
compileTime
points to the same object present in the String Pool.
Hence,
compileTime == literal returns true.
------------------------------------------------------------
Now consider
String runtime = first + second;
Here,
first and second are variables.
Their values are known only when the program runs.
Therefore, Java creates a new String object during execution.
This object is not the same object present inside the String Pool.
Hence,
runtime == literal
returns false.
------------------------------------------------------------
Now look at
final String finalFirst = "Hello";
final String finalSecond = "World";
Since both variables are declared as final, their values cannot change.
The compiler treats them as compile-time constants.
Therefore,
finalFirst + finalSecond is optimized during compilation into
"HelloWorld"
Hence,
finalConcatenation == literal
returns true.
------------------------------------------------------------
Interview Notes
Remember these three rules.
Rule 1
Literal + Literal
↓
Compile Time
↓
Stored in String Pool
------------------------------------------------------------
Rule 2
Variable + Variable
↓
Runtime
↓
Creates a New String Object
------------------------------------------------------------
Rule 3
final Variable + final Variable
↓
Compile Time
↓
Uses String Pool
These three rules are frequently asked in Java interviews.
------------------------------------------------------------
Important Points
✔ Compile-time concatenation uses the String Pool.
✔ Runtime concatenation creates a new String.
✔ final variables behave like literals during compilation.
✔ == compares object references.
✔ equals() compares String contents.
------------------------------------------------------------
Common Mistakes
❌ Assuming every concatenation uses the String Pool.
❌ Forgetting that variables are evaluated at runtime.
❌ Confusing final variables with normal variables.
------------------------------------------------------------
Top Company Interview Focus
★★★★★ Oracle
★★★★★ Amazon
★★★★★ Microsoft
★★★★★ Google
★★★★★ Adobe
★★★★☆ Atlassian
★★★★☆ IBM
★★★★★ Infosys Specialist Programmer
★★★★★ TCS Digital
------------------------------------------------------------
Follow-up Interview Questions
1. What is compile-time concatenation?
2. What is runtime concatenation?
3. Why does final affect concatenation?
4. Which object is stored in the String Pool?
5. Why does runtime concatenation create a new object?
------------------------------------------------------------
Real World Use Cases
✔ JVM optimization
✔ Memory optimization
✔ Java compiler internals
✔ Performance tuning
✔ Technical interviews
------------------------------------------------------------
Practice Questions
1.
String s1 = "Java";
String s2 = "Programming";
String s3 = "JavaProgramming";
System.out.println((s1 + s2) == s3);
Predict the output.
------------------------------------------------------------
2.
final String s1 = "Java";
final String s2 = "Programming";
String s3 = "JavaProgramming";
System.out.println((s1 + s2) == s3);
Predict the output.
------------------------------------------------------------
3.
Why does adding the final keyword change
the result?
------------------------------------------------------------
*/