Wednesday, December 10, 2008

Java Basics

Java Programming Basics

In this chapter, you find the basics of writing simple Java programs. The programs you see in this chapter don't do anything very interesting; they just display simple information on a console (in Windows, that's a command-prompt window). You need to cover a few more chapters before you start writing programs that do anything worthwhile. But the simple programs you see in this chapter are sufficient to illustrate the basic structure of Java programs.

Be warned that in this chapter I introduce you to several Java programming features that are explained in greater detail in later chapters. For example, you see some variable declarations, a method, and even an if statement and a for loop. The goal of this chapter isn't to march you into instant proficiency with these programming elements, but just to introduce you to them.


REMEMBER

You can find all the code listings used in this book at http://www.dummies.com/go/javaaiofd2e.

Looking at the Infamous Hello, World! Program

Many programming books begin with a simple example program that displays the text “Hello, World!” on the console. In Book I, Chapter 1, I show you a Java program that does that to compare it with a similar program written in C. Now, take a closer look at each element of this program, shown in Listing 1-1.

Listing 1-1: The HelloApp Program
Image from book

public class HelloApp 1
{ 2
public static void main(String[] args) 3
{ 4
System.out.println("Hello, World!"); 5
} 6
} 7
Image from book

Later in this chapter, you discover in detail all the elements that make up this program. But first, I want to walk you through it word by word.

Lines 1 and 2 mark the declaration of a public class named HelloApp:

1

public: A keyword of the Java language that indicates that the element that follows should be made available to other Java elements. In this case, what follows is a class named HelloApp. As a result, this keyword indicates that the HelloApp class is a public class, which means other classes can use it. (In Book III, Chapter 2, I cover the most common alternative to public: private. There are also other alternatives, but they're covered in later chapters.)

class: Another Java keyword that indicates that the element being defined here is a class. All Java programs are made up of one or more classes. A class definition contains code that defines the behavior of the objects created and used by the program. Although most real-world programs consist of more than one class, the simple programs you see in this minibook have just one class.

HelloApp: An identifier that provides the name for the class being defined here. While keywords, such as public and class, are words that are defined by the Java programming language, identifiers are words that you create to provide names for various elements you use in your program. In this case, the identifier HelloApp provides a name for the public class being defined here. (Although identifier is the technically correct term, sometimes identifiers are called symbols or names.)

2

{: The opening brace on line 2 marks the beginning of the body of the class. The end of the body is marked by the closing brace on line 7. Everything that appears within these braces belongs to the class. As you work with Java, you'll find that it uses these braces a lot. Pretty soon the third and fourth fingers on your right hand will know exactly where they are on the keyboard.

Open table as spreadsheet

Lines 3 through 6 define a method of the HelloApp class named main:

3

public: The public keyword is used again, this time to indicate that a method being declared here should have public access. That means classes other than the HelloApp class can use it. All Java programs must have at least one class that declares a public method named main. The main method contains the statements that are executed when you run the program.

static: You find all about the static keyword in Book III, Chapter 3. For now, just take my word that the Java language requires that you specify static when you declare the main method.

void: In Java, a method is a unit of code that can calculate and return a value. For example, you could create a method that calculates a sales total. Then the sales total would be the return value of the method. If a method doesn't need to return a value, you must use the void keyword to indicate that no value is returned. Because Java requires that the main method not return a value, you must specify void when you declare the main method.

main: Finally, the identifier that provides the name for this method. As I've already mentioned, Java requires that this method be named main. Besides the main method, you can also create additional methods with whatever names you want to use. You discover how to create additional methods in Book II, Chapter 7. Until then, the programs consist of just one method named main

(String[] args): Oh boy. This Java element is too advanced to thoroughly explain just yet. It's called a parameter list, and it's used to pass data to a method. Java requires that the main method must receive a single parameter that's an array of String objects. By convention, this parameter is named args. If you don't know what a parameter, a String, or an array is, don't worry about it. You can find out what a String is in the next chapter, and parameters are in Book II, Chapter 7; arrays in Book IV. In the meantime, realize that you have to code (String[] args) on the declaration for the main methods in all your programs.

4

Another {: Another set of braces begins at line 4 and ends at line 6. These mark the body of the main method. Notice that the closing brace in line 6 is paired with the opening brace in line 4, while the closing brace in line 7 is paired with the one in line 2. This type of pairing is commonplace in Java. In short, whenever you come to a closing brace, it is paired with the most recent opening brace that hasn't already been closed-that is, that hasn't already been paired with a closing brace.

5

System.out.println("Hello, World!");: This is the only statement in the entire program. It calls a method named println that belongs to the System.out object. The println method displays a line of text on the console. The text to be displayed is passed to the println method as a parameter in parentheses following the word println. In this case, the text is the string literal Hello, World! enclosed in a set of quotation marks. As a result, this statement displays the text Hello, World! on the console.

Note that in Java, statements end with a semicolon. Because this is the only statement in the program, this line is the only one that requires a semicolon.

6

}: Line 6 contains the closing brace that marks the end of the main method body that was begun by the brace on line 4.

7

Another }: Line 7 contains the closing brace that marks the end of the HelloApp class body that was begun by the brace on line 2. Because this program consists of just one class, this line also marks the end of the program.

Open table as spreadsheet

To run this program, you must first use a text editor to enter it-exactly as it appears in Listing 1-1-into a text file named HelloApp.java. Then you can compile it by running the following command at a command prompt:

javac HelloApp.java

This command creates a class file named HelloApp.class that contains the Java bytecodes compiled for the HelloApp class.

You can run the program by entering this command:

java HelloApp

Now that you've seen what a Java program actually looks like, you're in a better position to understand exactly what this command does. First, it loads the Java Virtual Machine into memory. Then it locates the HelloApp class, which must be contained in a file named HelloApp.class. Finally, it runs the main method of the HelloApp class. The main method, in turn, displays the message “Hello, World!” on the console.

The rest of this chapter describes some of the basic elements of the Java programming language in greater detail.

No comments:

Post a Comment