Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

Beginning the Program

Using your word processor, begin your Java programming career by entering each line from Listing 2.1. Don't enter the line number and colon at the beginning of each line— these are used in this book so that specific line numbers can be referred to.

Example 2.1. The Saluton Program

1: 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 some lines. When you're done, save the file with the file name Saluton.java.

At this point, Saluton.java contains the bare-bones form of a Java program. You will create several programs that start off 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 should also make sense—it's a sentence in actual English. The rest is completely new, however, and each part is introduced in the following sections.

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 are also called classes.

In this example, the program name Saluton matches the file name you gave your document, Saluton.java. As a rule, a Java program should have a name that matches the first part of its filename, and they should be capitalized in the same way.

If the program name doesn't match the filename, you will get an error when you try to compile some Java programs, depending on how the class statement is being used to configure the program. Although some programs can have a filename that doesn't match its program name, this makes it more difficult to work with the file later on.

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 will be handled first.

The main statement is the entry point to almost all Java programs. The exception are applets, programs that are run in conjunction with a World Wide Web page. Most of the programs you will write during the next several hours use main as the starting point.

Those Squiggly Bracket Marks

In the Saluton program, every line except Line 3 contains a squiggly bracket mark of some kind—either an { or an }. 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 will always 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 here (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 that is inside the main statement's block is a command for the computer to handle when the program is run.

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—it is put in the program solely for the benefit of humans who are looking at the program's text. 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 will happen. The reason is that you have not told the computer to do anything yet. The main statement block contains only a line of comments, which is ignored. If the Saluton program is going to greet anyone, you will have to add some commands inside the opening and closing brackets of the main statement block.

Share ThisShare This

Informit Network