-Now let's look at how we can do debugging within - -2
-Eclipse. I created a new file called AddNumbers which I'm - -3
-showing here. It takes two numbers, parses them into integers, - -4
-adds them and prints the sum, supposedly, of the two numbers. - -5
-Now we look at the run configuration for this program, - -6
-and here you can see that we're passing two arguments, - -7
-two and five, to the program. So now let's run - -8
-our program and see what happens. And the result says that - -9
-2 plus 5 is equal to 10, which is not - -10
-exactly correct. So we need to debug our program. We - -11
-need to figure out what's wrong with the program, why - -12
-the wrong result was, produced. So we're going to add a break - -13
-point here by double-clicking here on the side of the - -14
-code. And the break point is basically a place where I'm - -15
-telling my debugger to stop during the execution because I - -16
-want to inspect the state of the program. So to start - -17
-debugging, we select Debug as Java Application from the Context - -18
-menu, similar to what we were doing for running the program. - -19
-And as you can see, this asks us whether we want - -20
-to pass to the debug perspective, which is a, a perspective - -21
-specifically designed for debugging. We say yes. And as you - -22
-see here, it shows us, it's like a different, set of - -23
-views, so we can see the code down here with an - -24
-indication of where the execution is. And of course the execution - -25
-stopped at the break point, which is exactly where - -26
-we told the debugger to stop. So let's look at - -27
-some of the other views in this perspective. The view - -28
-here on the right-hand side, for example, shows the variables - -29
-in scope and the break points that are currently active - -30
-for the debugging session. This is where the editor is - -31
-at. The outline of the program and the console at - -32
-the bottom. So now let's execute one line by clicking - -33
-on the Step Over button here at the top, and this will - -34
-execute the line that is currently highlighted and therefore it will move to - -35
-the next line. And as you can see, one nice feature is that - -36
-if I move the mouse over a variable, I can see the value - -37
-of the variable. And the same thing I can do if I look - -38
-at the variables windows here on the right. If I click it, it - -39
-will tell me what is the value of the variable, and in case - -40
-of more complex variables you can even expand it and get more details. - -41
-So now let's step over another line by clicking again this button, - -42
-and as you can see now we get to the line that - -43
-is actually performing the sum, supposedly, so now let's do the same - -44
-thing that we did before, and let's mouse over b, and we can - -45
-see that the value of b is five, as expected. So now - -46
-let's step over this line as well, and execute the actual sum. And - -47
-doing the mouseover thing, we can see that the value of sum - -48
-is ten, which is not right, of course. In fact, if we check - -49
-a gain we can see that value of A is two. The - -50
-value of B is five and therefore it's clear that there's something - -51
-wrong going on here, and at this point we can notice that - -52
-here we are doing multiplication instead - -53
-of addition. And therefore that's what the - -54
-error is. And this is clearly a very simple case. Right? A - -55
-case in which probably you just needed to look at the code and - -56
-you didn't need the debugger. But you probably got the idea right? - -57
-So this can be extremely useful when you're debugging, when you're studying more - -58
-complex programs. If you want to stop the debugger because you're - -59
-done with your debugging session as in this case, you can - -60
-either click here on the Terminate button or you can also - -61
-just simply tell the debugger to continue the execution, to resume - -62
-the execution until the program terminates naturally. So, in this case, - -63
-we're going to click here just to show what happens. And what - -64
-happens is that, you know, the execution will just continue until - -65
-the program exits. So now let's say that we want to fix - -66
-this problem that we just discovered. So we replace the multiplication - -67
-with an addition, we save the program, and we execute the - -68
-program again by clicking on this button. And at this point, - -69
-unsurprisingly, we get the right result as shown in the console. -- cgit 1.4.1