Beginning the Program

NetBeans groups related programs together into a project. If you don’t have the Java24 project open, here’s how to retrieve it:

• Choose File, Open Project.

• Find and select the NetBeansProjects folder (if necessary).

• Choose Java24 and click Open Project.

The Java24 project appears in the Projects Pane.

To add a new Java program to the current project, choose File, New File. The New File Wizard opens, as shown in Figure 2.1.

Figure 2.1. The New File Wizard.

Image

The Categories pane lists the different kinds of Java programs you can create. Click the Java folder in this pane to see the file types that belong to this category. For this first project, choose the Empty Java File type, and click Next.

In the Class Name field, enter Saluton and click Finish to create the new Java program. An empty file named Saluton.java opens in the source code editor.

Using the source editor, begin your Java programming career by entering each line from Listing 2.1. These statements are called the program’s source code.


Caution

Don’t enter the line number and colon at the beginning of each line—these are used in this book to reference specific line numbers.


Listing 2.1. The Saluton Program


 1: public class Saluton {
 2:     public static void main(String[] arguments) {
 3:         // My first Java program goes here
 4:     }
 5: }


Make sure to capitalize everything exactly as shown, and use your spacebar or Tab key to insert the blank spaces in front of Lines 2–4. When you’re done, choose File, Save or click the Save All Files button to save the file.

At this point, Saluton.java contains the bare-bones form of a Java program. You will create several programs that start exactly like this one, except for the word Saluton on Line 1. This word represents the name of your program and changes with each program you write. Line 3 also should make sense—it’s a sentence in actual English. The rest is probably new to you.

The class Statement

The first line of the program is the following:

class Saluton {

Translated into English, this line means, “Computer, give my Java program the name Saluton.”

As you might recall from Hour 1, each instruction you give a computer is called a statement. The class statement is the way you give your computer program a name. It’s also used to determine other things about the program, as you will see later. The significance of the term class is that Java programs also are called classes.

In this example, the program name Saluton matches the document’s file name, Saluton.java. A Java program must have a name that matches the first part of its filename and should be capitalized the same way.

If the program name doesn’t match the filename, you get an error when you try to compile some Java programs, depending on how the class statement is being used to configure the program.

What the main Statement Does

The next line of the program is the following:

public static void main(String[] arguments) {

This line tells the computer, “The main part of the program begins here.” Java programs are organized into different sections, so there needs to be a way to identify the part of a program that is handled first.

The main statement is the entry point to most Java programs. The most common exceptions are applets, programs that are run as part of a web page, and servlets, programs run by a web server. Most programs you write during upcoming hours use main as their starting point.

Those Squiggly Bracket Marks

In the Saluton program, every line except Line 3 contains a squiggly bracket mark of some kind—either a { or a }. These brackets are a way to group parts of your program (in the same way that parentheses are used in a sentence to group words). Everything between the opening bracket { and the closing bracket } is part of the same group.

These groupings are called blocks. In Listing 2.1, the opening bracket on Line 1 is associated with the closing bracket on Line 5, which makes your entire program a block. You use brackets in this way to show the beginning and end of your programs.

Blocks can be located inside other blocks (just as parentheses are used in this sentence (and a second set is used here)). The Saluton program has brackets on Line 2 and Line 4 that establish another block. This block begins with the main statement. Everything inside the main statement’s block is a command for the computer to handle when the program is run.


Note

NetBeans can help you figure out where a block begins and ends. Click one of the brackets in the source code of the Saluton program. The bracket you clicked turns yellow along with its corresponding bracket. The Java statements enclosed within these yellow brackets comprise a block.


The following statement is the only thing located inside the block:

// My first Java program goes here

This line is a placeholder. The // at the beginning of the line tells the computer to ignore this line because it was put in the program solely for the benefit of humans who are looking at the source code. Lines that serve this purpose are called comments.

Right now, you have written a complete Java program. It can be compiled, but if you run it nothing happens. The reason why is that you haven’t told the computer to do anything yet. The main statement block contains only a single comment, which is ignored. You must add some statements inside the opening and closing brackets of the main block.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.24.36