Java Reference
In-Depth Information
so they allow you to provide information to humans that can be ignored by the computer. Here,
comments are used to indicate the purpose of the class. Single‐line comments begin with // and
function the same way. In Eclipse, unlike in very basic text editors, comments are colored to set
them apart from other parts of the code. Block comments are displayed in blue while single‐line
comments are displayed in green.
5. Next you have a class declaration public class MyFirstApplication , and you'll find the body
of the class between curly brackets { and } . The public modifier indicates that classes outside
the package can access this class. This concept was introduced in Chapter 2 but will be covered in
more detail in Chapter 4.
6. The first statement in your class is the declaration of your main method: public static void
main (String[] args) . Remember from Chapter 2 that the main method is the entry point for
execution. When a program runs, it will start from the main method. Sometimes you will see a
notification from Eclipse that it is searching for the main method before it can run a program. You
may also see an error if the main method cannot be found. You should always check to be sure
that your program has a main method (if you want to run it) and that the declaration is correct and
complete. It should always start: public static void main (String[] args){ .
7. Inside the main method, between an inner set of curly brackets, you find the statements to be
executed. In this small example, there is only one statement: System.out.println("I've coded,
compiled, and run my first Java program!"); . This prints the text you see between the
parentheses to the standard output. In Eclipse, the standard output is the console.
8. The last two lines contain the closing bracket of the main method and the closing bracket of the
class.
9. Unlike the exercise using the command window, Eclipse manages the compilation and execution
of your program for you. When you click Run, the .java source file is compiled to a .class
machine‐readable file. After compilation, the program is executed.
10. After execution, you should have seen the text I've created, compiled, and run my first
Java program! printed to the Console tab in the bottom view of your workbench.
11. If there were any errors, you can simply edit the class file in the Code Editor view and re‐run the
program. Also, if there were typos or other mistakes, Eclipse can often highlight these the way a
word processing program will show you misspellings and grammatical errors. The code checker
and the seamless integration of coding, compiling, and executing are some of the many benefits a
new programmer will enjoy when using an IDE compared to command‐line programming.
In addition to the menu bar and right‐click options, Eclipse has a row of buttons you can use for
many common operations. Some of the buttons are shown in the next few figures with a short
description of what they do. The first set of buttons on the far left are from the File menu. The first,
an icon of a window with a yellow plus sign, allows you to create new projects, packages, classes,
and so on. The next looks similar but shows some details inside the window icon; this is for creating
new graphical components. The icon of a single floppy disk is the equivalent of choosing File Save.
The icon of a stack of three floppy disks is the equivalent of choosing File Save All. The last one in
this panel, an icon of a printer, is for printing. These icons are shown in Figure 3-18.
Search WWH ::




Custom Search