What is an IDE (Integrated Development Environment)? Which ones are good for beginners and which are good for intermediate programmers? What is Eclipse? How and where can you download it? All this and more can be found in my previous article by clicking here.
This is where I left off in my article Getting Started with Coding in Java:
The Eclipse Tool Bar
These are the buttons we are going to use (Shortcuts for mac):
Compiling and Fixing Your Code
Unlike jGrasp, eclipse auto compiles your code so you, as the programmer, do not need to worry about the compile step of running your code. This is very beneficial because it checks your code, like a word processor does, as you code. So if you make a mistake, like below, it tells you exactly where it thinks the error is.
Note: this is for coding errors, not logic errors. Your code may compile perfectly and not do what it is supposed to do.
As you can see the code has a red underline under a section. This is because I wrote a “:” instead of a “;”. Semicolons end every line of code that does not end in “{“ or “}”. As you can see, there is also a red “x” on the left side as a further marker to make it easier to spot errors.
Eclipse’s automatic compiler lets the user fix their code as they write it.
Running Your Code
As aforementioned when running your code in eclipse you do not need to worry about compiling your code, because eclipse does it for you. All you have to do is save the class (file type for runnable code) the run it by either pressing the run button or by using the short cut.
Debugging Your Code
Debugging is the act of removing bugs (errors) from your code. We do this by going through the code line by line as opposed to letting the code run completely. We do this by using break points. To place a break point you simply have to double-click on the left side of the line of code you wish to break at. Like so:
Breaks are used as points of reference to look into possible errors. Your code will stop at each break point allowing you to see if the code is running smoothly, up to that point, and to locate the source of the error. Obviously debugging is more for logic errors and less for syntax errors, for if there was a syntax error it would likely be caught by the compiler. You can use debugging to locate syntax errors but it is more for less obvious errors.
Eclipse has a special debugging mode for its layout. The first time you use the debugging mode it will ask if you wish to switch to this layout for debugging.
This layout makes it easier to see the important details while debugging.
To switch in and out of this mode click the Java or Debug button in the top right:
Special Features of Eclipse
Eclipse has some special features designed to help make the programmer’s life easier.
One such feature is automatic closing of brackets. When you open bracket it automatically creates its pair for you. The only exception is “{“ which only gets closed, “}”, when you hit enter after it. This is because these brackets are used to define groups of code.
Another feature is a (by command) format/indent of your code. You can do this this by selecting a segment or all of your code and pressing ⌘-i. This will format your code neatly so it easy to read for human eyes.
Eclipse can auto generate some code for you. As we saw in my previous article, you can get eclipse to auto generate a class with a main method already in it. When we get to more high level programming, we can get eclipse to generate some code that tends to be repetitive. These are called getters and setters. I will explain further when we get to that. Just know you can use the source tab to find a way to generate them.
One of my favourite features is an auto completion feature that allows you to ask eclipse for suggestions on what to write next and it will fill in the code for you. You do this by pressing ctrl-space (Mac). This brings up a menu of options which you can select from by hitting the enter button.
Now that you know how to use the eclipse environment, you are now ready to learn how to code in Java.