Tuesday, August 23, 2016

JAVA 3 - Debugging. The code doesn't bother me anyway.

Last time we looked at all the ways that your development environment will try and keep you on the right path.  I promised to teach you another useful tool - so today we're going to use the debugger.



Now I need you to build a brand new project - which you can call whatever you like, and copy into it the code from here.  Which looks like this ...


Unlike yesterday, this has a whole series of print commands, and "other things", which I'm not going to explain right now.

Sometimes code goes wrong, and the only way to be sure is to instead of running and watching it fail, you run it in a debugger.  A debugger allows you to run part of your code a line at a time, and to also keep an eye on the data.

This article is going to introduce you to the basics.

Set a breakpoint

Choose line 25 which has the line "Can't hold it back anymore".  Choose the far left edge and right click as below, then select "Toggle Breakpoint".


Once set, you'll see a blue dot next to the line - this is our breakpoint.  When the system is run in the debugger, it will run to this point, then wait.


Run in debug

To run in debug select Run -> Debug from the top menu.  You'll be asked if it's okay to change your perspective, and select OK.

Your view should now look like this...




You will notice the line we've selected is highlighted (it's not run yet).

Under the console, you can only see the first two lines have been printed,



Similarly, in the top right, there's a panel labelled "variables" which shows that line_count is 2.


This area allows you to keep track of your data items used by your code.

Move through the debugger

Press F6 - this is the "step over" button.  You'll notice the highlighted line moves down.

Every time it passes over a println command, a new line of the song is printed.  Every time it passes over the line_count++ command, the line_count variable in the top right increases by one.

Later on when we've covered methods, you can chose to "step over" or "step into" methods,

  • Step over [F6] means you just have the method execute and return
  • Step into [F5] means you follow the method, executing line-by-line as we're doing now


When you've had enough select to resume with the F8 command.  The rest of your program will execute.



Have a go again until you're familiar.  When you're done, return to normal view by selecting the Java view button in the top right, next to Quick Access.


And there you go.  Combined with last time's look at how the compiler will complain to you, this should be handy for when things go a little wrong for you!





Note on naming standards

I've never been particularly good at remembering this - but as Srinivas pointed out in the comments last time - class names should always begin with a capital letter (Pascal casing).

Method names should begin with lower case names (camel casing).

This doesn't affect the code, but just helps to make it easier to read if you're talking about a class or a method just by if it starts with a capital or not.

So main() is a good example of a method, has to start with lower case.

My previous classes though - ayeUpWorld and letItGo should really have started with a capital letter - it's actually relatively easy to do this in Eclipse, but not particularly obvious.  Right click on your class in Package Explorer and go Refactor -> Rename.  It will rename not just the class but any instances you're using of the class.  Simples.




1 comment:


  1. Entertainment center that offers a variety of casino games.

    จีคลับ Betting Online Gambling Luck Online at our online gambling site you will be able to help you make the most of online gambling games. Lots of online lottery To make you an online gaming investor, all investors.


    In addition, the player who uses online gambling services with us, the player is able to gambling. We are the investor. Register with us. Without downloading the application, you can join the online betting with us unknowingly. Just have the internet, you can play anywhere anytime. Gambling games are very valuable. We also have jackpots that will give you a lot of money if you are interested in becoming part of our online betting site. And today, the players who come to the investment service on our website, you will receive a lot of interest, you are interested in investing here. บาคาร่า

    ReplyDelete