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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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
1
00:00:00,000 --> 00:00:03,000
Hello team, Today I will reveal 
the secret which will help you 

2
00:00:03,000 --> 00:00:06,000
to become senior developer. Today I 
will show you how debugger works 

3
00:00:06,000 --> 00:00:10,000
and how you can investigate execution 
of your program to find the 

4
00:00:10,000 --> 00:00:14,000
defects and to improve your code. 
What is a debugger? Debugger is 

5
00:00:14,000 --> 00:00:18,000
a special tool which helps you 
to run program under controlled conditions 

6
00:00:18,000 --> 00:00:22,000
that allow you to monitor all 
changes in your program. You can 

7
00:00:22,000 --> 00:00:27,000
pause execution of your program whenever 
you want, you can display contents 

8
00:00:27,000 --> 00:00:31,000
of memory, you can investigate state 
of variables and lots more. How 

9
00:00:31,000 --> 00:00:35,000
this will help you to become 
senior developer? Debugger tool helps you 

10
00:00:35,000 --> 00:00:40,000
to understand how even complex enterprise 
programs work. You can spend some 

11
00:00:40,000 --> 00:00:44,000
time to investigate what is happening 
inside the program – what methods 

12
00:00:44,000 --> 00:00:47,000
are called when checkout is happening 
on the website, you can see 

13
00:00:47,000 --> 00:00:51,000
what arguments are passed to the 
method and what are their values, 

14
00:00:51,000 --> 00:00:55,000
probably there are specific logic for 
discount calculation is also on place. 

15
00:00:55,000 --> 00:00:59,000
This will help you to understand 
where to add additional logic, or 

16
00:00:59,000 --> 00:01:03,000
in other words – where to 
write new code for tax calculation, 

17
00:01:03,000 --> 00:01:07,000
let’s say, or where you need 
to fix defects. Even now, when 

18
00:01:07,000 --> 00:01:10,000
you did your home task, I’m 
sure there were cases when you 

19
00:01:10,000 --> 00:01:14,000
run you program, and you didn’t 
understand why console output is not 

20
00:01:14,000 --> 00:01:18,000
the one you expected. To get 
answer on ‘why’ question and to 

21
00:01:18,000 --> 00:01:22,000
understand how program works – debugger 
tool exists and today, I will 

22
00:01:22,000 --> 00:01:25,000
teach you how you can use 
it. As always, to understand how 

23
00:01:25,000 --> 00:01:29,000
specific tool works – let’s jump 
to practice. For example, let’s try 

24
00:01:29,000 --> 00:01:32,000
to implement one of the home 
tasks. Do you remember home task 

25
00:01:32,000 --> 00:01:36,000
when you needed to filter a 
string by words length? Let’s debug 

26
00:01:36,000 --> 00:01:39,000
this program. The first thing you 
need to do is to set 

27
00:01:39,000 --> 00:01:44,000
a breakpoint. Breakpoint is a line 
in your program where execution will 

28
00:01:44,000 --> 00:01:48,000
be paused. Let’s set the breakpoint 
in this line. Just hover mouse 

29
00:01:48,000 --> 00:01:52,000
over this row and do mouse 
double left click. To debug your program, 

30
00:01:52,000 --> 00:01:56,000
you need to do mouse right 
click and press ‘Debug As’ -> 

31
00:01:56,000 --> 00:02:01,000Java Application’. Our program waits for 
user input in this line. Let’s 

32
00:02:01,000 --> 00:02:09,000
enter some text here. Now our 
program expects another user input in 

33
00:02:09,000 --> 00:02:14,000
this line. Minimum word length will 
be four. And now program execution 

34
00:02:14,000 --> 00:02:18,000
paused in the line where we 
set breakpoint. Eclipse asks a permission 

35
00:02:18,000 --> 00:02:23,000
to switch perspective. Let’s switch perspective 
to debug perspective, because there are 

36
00:02:23,000 --> 00:02:27,000
a lot of useful views on 
it. So now we switched to 

37
00:02:27,000 --> 00:02:30,000
debug perspective. Here on the right 
you always can track how state 

38
00:02:30,000 --> 00:02:35,000
of the variables are changed. For 
example, at this moment of program 

39
00:02:35,000 --> 00:02:40,000
execution, we have: args variable 
which is empty; Scanner object;

40
00:02:40,000 --> 00:02:45,000
; userInput String - and minLength 
variable. Let’s pretend that we want 

41
00:02:45,000 --> 00:02:50,000
to understand which regular expression works 
the best for this particular case. 

42
00:02:50,000 --> 00:02:53,000
Or we want to make sure 
that this regular expression works as 

43
00:02:53,000 --> 00:02:59,000
expected. Let’s select expression -> mouse 
right click -> watch. Here in 

44
00:02:59,000 --> 00:03:03,000
expression tab we see the result 
of execution of this expression. Let’s 

45
00:03:03,000 --> 00:03:08,000
add new with another regular expression, 
probably, we just need to split 

46
00:03:08,000 --> 00:03:14,000
userInput by space character? Click here 
– ‘Add new expression’ – userInput, 

47
00:03:14,000 --> 00:03:19,000
split, space character. And we can 
see the result. In this case 

48
00:03:19,000 --> 00:03:23,000
empty strings will be present in 
our result array. You can also 

49
00:03:23,000 --> 00:03:27,000
use debug shell. Let me show 
you one more example. You can 

50
00:03:27,000 --> 00:03:31,000
select expression, mouse right click and 
press ‘display’ to see the result 

51
00:03:31,000 --> 00:03:36,000
right here. Additionally, you can press 
Ctrl Shift D to switch to 

52
00:03:36,000 --> 00:03:40,000
debug shell view. Here it is. 
What is good about debug shell? 

53
00:03:40,000 --> 00:03:45,000
There is autosuggestion support here. Let’s 
remove this by selecting and clicking 

54
00:03:45,000 --> 00:03:51,000
backspace. Now I can start type 
‘user’ and press Ctrl plus Space 

55
00:03:51,000 --> 00:03:55,000and here is autosuggestion. Now 
I can enter dot, and again 

56
00:03:55,000 --> 00:04:00,000
autosuggestion works as expected. Let’s again 
split userInput by space character. To 

57
00:04:00,000 --> 00:04:03,000
execute code in debug shell you 
need to select it and press 

58
00:04:03,000 --> 00:04:08,000
this button – ‘Display result’. And 
again, you can clear it if 

59
00:04:08,000 --> 00:04:12,000
you wish. What else you can 
do here? It is very useful 

60
00:04:12,000 --> 00:04:15,000
in debug mode to check your 
code with different values and to 

61
00:04:15,000 --> 00:04:19,000
change them during the program execution. 
For example, let’s check our program 

62
00:04:19,000 --> 00:04:29,000
with different userInput. userInput equals and 
specify any random string here. Now, 

63
00:04:29,000 --> 00:04:33,000
you can execute this code. Select 
it and press this button – 

64
00:04:33,000 --> 00:04:37,000Execute selected text’. Let’s switch to 
variables view here and you can 

65
00:04:37,000 --> 00:04:42,000
see that our userInput variable has 
been changed. So, during the debugging 

66
00:04:42,000 --> 00:04:46,000
you can execute code which doesn’t 
exist in your current program. That 

67
00:04:46,000 --> 00:04:49,000
can help you a lot, when 
you want to make sure whether 

68
00:04:49,000 --> 00:04:53,000
you need to add some code 
or no. Definitely you can check 

69
00:04:53,000 --> 00:04:56,000
this in different ways, for example 
with unit tests, or just add 

70
00:04:56,000 --> 00:05:00,000
some code and run your program 
one more time. But when you 

71
00:05:00,000 --> 00:05:04,000
deal with huge programs you realize 
that making changes in one line 

72
00:05:04,000 --> 00:05:08,000
and compile all files, build the 
.jar or .war file to deploy 

73
00:05:08,000 --> 00:05:10,000
it on web server or just 
to run it – this may 

74
00:05:10,000 --> 00:05:15,000
take up to thirty minutes. That’s 
why such manipulations in debug mode 

75
00:05:15,000 --> 00:05:20,000
can help you be more efficient 
and productive. Ok, let’s move on. 

76
00:05:20,000 --> 00:05:23,000
Here on top you have buttons 
to control execution of your program. 

77
00:05:23,000 --> 00:05:29,000
This play button means jump to 
another breakpoint. Stop button obviously means 

78
00:05:29,000 --> 00:05:34,000stop’ execution. And here are two 
buttons which we will use. Step 

79
00:05:34,000 --> 00:05:38,000
into and step over. Step over 
brings us to the next line 

80
00:05:38,000 --> 00:05:42,000
of the execution. Let’s press it. 
In this line I see that 

81
00:05:42,000 --> 00:05:46,000
filterWordsByLength method is going to be 
executed. And I want to investigate 

82
00:05:46,000 --> 00:05:51,000
what is happening inside it. I 
press ‘step into’ button. This button 

83
00:05:51,000 --> 00:05:56,000
brings me inside this method. You 
can also notice that variables list 

84
00:05:56,000 --> 00:06:00,000
here is changed. Now I can 
see only variables which are visible 

85
00:06:00,000 --> 00:06:05,000
in scope of this method. Let’s 
press ‘step over’ button multiple times. 

86
00:06:05,000 --> 00:06:08,000
And you see how counter variable 
is changed. When you are tired 

87
00:06:08,000 --> 00:06:12,000
of this loop, you can set 
one more breakpoint. Let’s say after 

88
00:06:12,000 --> 00:06:16,000
this loop. Now I can press 
‘Play’ button and jump to another 

89
00:06:16,000 --> 00:06:21,000
breakpoint. Now I can press ‘step 
over’ multiple times and see filtered 

90
00:06:21,000 --> 00:06:28,000
words even without printing them to 
console. Press ‘Play’ button – to 

91
00:06:28,000 --> 00:06:32,000
finish the program. Important thing which 
students always ask me is how 

92
00:06:32,000 --> 00:06:37,000
to restore previous view in eclipse. 
Just navigate back to Java or 

93
00:06:37,000 --> 00:06:41,000
Java EE perspective here. That’s it. 
So now, you know how to 

94
00:06:41,000 --> 00:06:44,000
debug program of any level of 
complexity. This will help you a 

95
00:06:44,000 --> 00:06:50,000
lot in finding bugs and in investigation 
how program works. That’s all for today. 

96
00:06:50,000 --> 00:06:54,000
Thank you for your attention and 
see you in the next lesson.