{"id":788,"date":"2020-06-08T16:57:05","date_gmt":"2020-06-08T16:57:05","guid":{"rendered":"http:\/\/techforhumans.site\/?p=788"},"modified":"2023-12-24T16:32:15","modified_gmt":"2023-12-24T16:32:15","slug":"debugging-in-eclipse","status":"publish","type":"post","link":"https:\/\/techforhumans.site\/debugging-in-eclipse\/","title":{"rendered":"Debugging in Eclipse"},"content":{"rendered":"\n

What is Debugging?<\/h4>\n\n\n\n

Debugging of the code helps us to understand execution of the program line by line. In debugging we can see what is value of variables after each line which can help us understand logic of code and identify errors or bugs.<\/p>\n\n\n\n

Debugging can be done manually using pen and paper<\/a> or by using tools such as Eclipse.<\/p>\n\n\n\n

How to debug in Eclipse step by step:<\/h4>\n\n\n\n

Step 1 :<\/strong> Create a new program with class name ‘Tech4HumansDebug’ and write given below code :<\/p>\n\n\n

\n  \npublic class Tech4HumansDebug {\n\n\tstatic int sumMethod1(int num1, int num2) {\n\t\tint sum = 0;\n\t\tsum = num1 + num2;\n\t\treturn sum;\n\t}\n\n\tstatic int sumMethod2(int a, int b) {\n\t\tint sum = 0;\n\t\tfor (int i = 0; i < b; i++) {\n\t\t\ta = a + 1;\n\t\t}\n\t\tsum = a;\n\t\treturn sum;\n\t}\n\n\tpublic static void main(String[] args) {\n        \n\t\tint x1 = 2;\n\t\tint y1 = 3;\n\t\t\n\t\tint sum1 = sumMethod1(x1, y1);\n\t\tint sum2 = sumMethod2(5, 4);\n\n\t\tSystem.out.println("sum from method1 = " + sum1);\n\t\tSystem.out.println("sum from method2 = " + sum2);\n\t}\n\n}\n<\/pre><\/div>\n\n\n

Step 2 : <\/strong>We have to choose a place in code where we need to start debugging our code and insert a breakpoint. A breakpoint is the line of code from where we need to debug our code and proceed manually. <\/p>\n\n\n\n

To insert a breakpoint right click at the left hand side of program where line numbers are written and click on ‘Toggle Breakpoint’ as shown in below image. A breakpoint will be inserted. Alternatively we can also double click on the line number to insert breakpoint.<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

Step 3 :<\/strong> Similarly insert breakpoint at line 21 of our code too. There should be 2 circles(breakpoints) inserted at line 6 and 21 as shown below.<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

Step 4 :<\/strong> Now to start debugging the code right click on the code and select ‘Debug as’ and then ‘Java Application’.<\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

Step 5 :<\/strong> A new dialog box will appear which will ask to change perspective. Click on ‘Switch’ button. <\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n

Step 6 :<\/strong> By changing perspective some additional windows will appear on eclipse which will help you debug code easily. The statement with first breakpoint will be highlighted with a green debug pointer. On the tool bar few buttons will be enabled too which were previously disabled. Let us understand one by one.<\/p>\n\n\n

\n
\"\"\/<\/figure><\/div>\n\n\n

At the right hand side a new window will appear having 3 tabs ‘Variables’, ‘Breakpoints’, ‘Expressions’. <\/strong><\/p>\n\n\n

\n
\"\"<\/figure><\/div>\n\n\n