java-development-for-beginners-learnit / 12 - Debugging Tools /001 How to debug Java programs_en.srt
Tan115's picture
Add files using upload-large-folder tool
f99add3 verified
Raw
History Blame
10 kB
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,000
Java 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,000
and 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,000
Execute 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,000
stop’ 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.