Now let's look at how we can do debugging within
Eclipse. I created a new file called AddNumbers which I'm
showing here. It takes two numbers, parses them into integers,
adds them and prints the sum, supposedly, of the two numbers.
Now we look at the run configuration for this program,
and here you can see that we're passing two arguments,
two and five, to the program. So now let's run
our program and see what happens. And the result says that
2 plus 5 is equal to 10, which is not
exactly correct. So we need to debug our program. We
need to figure out what's wrong with the program, why
the wrong result was, produced. So we're going to add a break
point here by double-clicking here on the side of the
code. And the break point is basically a place where I'm
telling my debugger to stop during the execution because I
want to inspect the state of the program. So to start
debugging, we select Debug as Java Application from the Context
menu, similar to what we were doing for running the program.
And as you can see, this asks us whether we want
to pass to the debug perspective, which is a, a perspective
specifically designed for debugging. We say yes. And as you
see here, it shows us, it's like a different, set of
views, so we can see the code down here with an
indication of where the execution is. And of course the execution
stopped at the break point, which is exactly where
we told the debugger to stop. So let's look at
some of the other views in this perspective. The view
here on the right-hand side, for example, shows the variables
in scope and the break points that are currently active
for the debugging session. This is where the editor is
at. The outline of the program and the console at
the bottom. So now let's execute one line by clicking
on the Step Over button here at the top, and this will
execute the line that is currently highlighted and therefore it will move to
the next line. And as you can see, one nice feature is that
if I move the mouse over a variable, I can see the value
of the variable. And the same thing I can do if I look
at the variables windows here on the right. If I click it, it
will tell me what is the value of the variable, and in case
of more complex variables you can even expand it and get more details.
So now let's step over another line by clicking again this button,
and as you can see now we get to the line that
is actually performing the sum, supposedly, so now let's do the same
thing that we did before, and let's mouse over b, and we can
see that the value of b is five, as expected. So now
let's step over this line as well, and execute the actual sum. And
doing the mouseover thing, we can see that the value of sum
is ten, which is not right, of course. In fact, if we check
a gain we can see that value of A is two. The
value of B is five and therefore it's clear that there's something
wrong going on here, and at this point we can notice that
here we are doing multiplication instead
of addition. And therefore that's what the
error is. And this is clearly a very simple case. Right? A
case in which probably you just needed to look at the code and
you didn't need the debugger. But you probably got the idea right?
So this can be extremely useful when you're debugging, when you're studying more
complex programs. If you want to stop the debugger because you're
done with your debugging session as in this case, you can
either click here on the Terminate button or you can also
just simply tell the debugger to continue the execution, to resume
the execution until the program terminates naturally. So, in this case,
we're going to click here just to show what happens. And what
happens is that, you know, the execution will just continue until
the program exits. So now let's say that we want to fix
this problem that we just discovered. So we replace the multiplication
with an addition, we save the program, and we execute the
program again by clicking on this button. And at this point,
unsurprisingly, we get the right result as shown in the console.