Previous Section  < Day Day Up >  Next Section

Creating the Saluton Program

The first Java program that you create will be an application that displays a traditional greeting from the world of computer science, "Saluton mondo!"

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.

Listing 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 filename Saluton.java.

Watch Out!

If you're using Windows, don't forget to put quotation marks around the filename when saving it, as in "Saluton.java". This will ensure that the file extension .txt is not added to the filename automatically.


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 also should make sense—it's a sentence in actual English. The rest is completely new, with each part 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 also are called classes.

In this example, the program name Saluton matches the filename you gave your document, Saluton.java. As a general 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.

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 most Java programs. The exceptions are applets, programs that are run in conjunction with a World Wide Web page, and servlets, programs run by a web server. Most programs you will write during upcoming 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 always will 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.

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 happens. 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 must add some commands inside the opening and closing brackets of the main statement block.

    Previous Section  < Day Day Up >  Next Section