Running a Java Program

To see whether the Saluton program. does what you want, run the class with the Java Virtual Machine (JVM), the interpreter that runs all Java code. In NetBeans, choose the menu command Run, Run File. An output pane opens below the source editor. In this pane, if there were no errors, the program displays the output, as shown in Figure 2.3.

Figure 2.3. Running your first Java program.

Image

Note

You might be asking yourself why “Saluton mondo!” is a traditional greeting. The phrase means “Hello world!” in Esperanto, an artificial language created by Ludwig Zamenhof in 1887 to facilitate international communication. It’s only a traditional greeting in the sense that I’m trying to start that tradition.


If you see the text “Saluton Mondo!” you have just written your first working Java program! Your computer has just greeted the world—a tradition in the computer programming field that’s as important to many of us as caffeine, short-sleeved dress shirts, and Call of Duty.

Summary

During this hour, you got your first chance to create a Java program using the NetBeans IDE. You learned that to develop a Java program you need to complete these four basic steps:

1. Write the program with a text editor.

2. Compile the program into a class file.

3. Tell the Java Virtual Machine to run the class.

4. Call your mother.

Along the way, you were introduced to some basic computer programming concepts such as compilers, interpreters, blocks, statements, and variables. These will become clearer to you in successive hours. As long as you got the Saluton program to work during this hour, you’re ready to proceed.

(The fourth step has nothing to do with Java programming. It’s just something my mother suggested I put in the book.)

Q&A

Q. How important is it to put the right number of blank spaces on a line in a Java program?

A. It’s completely unimportant. Spacing is strictly for the benefit of people looking at a computer program—the Java compiler couldn’t care less. You could have written the Saluton program without using blank spaces or used the Tab key to indent lines, and it would compile successfully.

Although the number of spaces in front of lines isn’t important, you should use consistent spacing in your Java programs. Why? Because spacing makes it easier for you to see how a program is organized and to which programming block a statement belongs.

Q. A Java program has been described as a class and as a group of classes. Which is it?

A. Both. The simple Java programs you create during the next few hours are compiled into a single file with the extension .class. You can run these with the Java Virtual Machine. Java programs also can be made up of a set of classes that work together. This topic is fully explored during Hour 10, “Creating Your First Object.”

Q. If semicolons are needed at the end of each statement, why does the comment line // My first Java program goes here not end with a semicolon?

A. Comments are completely ignored by the compiler. If you put // on a line in your program, this tells the Java compiler to ignore everything to the right of the // on that line. The following example shows a comment on the same line as a statement:

System.out.println(greeting); // hello, world!

Q. I couldn’t find any errors in the line where the compiler noted an error. What can I do?

A. The line number displayed with the error message isn’t always the place where an error needs to be fixed. Examine the statements that are directly above the error message to see whether you can spot any typos or other bugs. The error usually is within the same programming block.

Q. How can I visit Antarctica?

A. If you’re not willing to become a scientific researcher or a support staffer such as a cook, an electrician, or a doctor, you can become one of the 10,000 people who visit the frozen continent annually as tourists.

Flyovers are available from Australia, New Zealand, and South America and cost around $1,000 per person.

Several cruise ships visit for a trip lasting from 10 days to three weeks, the most expensive of which is around $25,000. Some cruises offer a chance to kayak or hike among penguins, visit icebergs, and even camp overnight.

The Polar Cruises website at www.polarcruises.com provides more information for prospective Antarctica visitors.

The British Antarctic Survey offers a piece of advice for visitors: “Do not walk onto glaciers or large snowfields unless properly trained.”

Workshop

Test your knowledge of the material covered in this hour by answering the following questions.

Quiz

1. When you compile a Java program, what are you doing?

A. Saving it to a disk

B. Converting it into a form the computer can better understand

C. Adding it to your program collection

2. What is a variable?

A. Something that wobbles but doesn’t fall down

B. Text in a program that the compiler ignores

C. A place to store information in a program

3. What is the process of fixing errors called?

A. Defrosting

B. Debugging

C. Decomposing

Answers

1. B. Compiling a program converts a .java file into a .class file or a set of .class files.

2. C. Variables are one place to store information; later you learn about others such as arrays and constants. Weebles wobble but they don’t fall down, and comments are text in a program that the compiler ignores.

3. B. Because errors in a computer program are called bugs, fixing those errors is called debugging. Some programming tools come with a tool called a debugger that helps you fix errors.

Activities

If you’d like to explore the topics covered in this hour a little more fully, try the following activities:

• You can translate the English phrase “Hello world!” into other languages using Yahoo’s Babelfish at http://babelfish.yahoo.com. Write a program that enables your computer to greet the world in a language such as French, Italian, or Portuguese.

• Go back to the Saluton program and add one or two errors. For example, take a semicolon off the end of a line or change the text println on one line to print1n (with a number 1 instead of the letter L). Save the program and try to compile it. Compare the error messages you get to the errors you caused.

To see solutions to these activities, visit the book’s website at www.java24hours.com.

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

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