Thursday, November 10, 2011

JAVA (II)

Anatomy of a Java Program

The application program in Listing 1.1 has the following components:
  • Comments
  • Reserved words
  • Modifiers
  • Statements
  • Blocks
  • Classes
  • Methods
  • The main method
To build a program, you need to understand these basic elements. They are explained in the sections that follow.

1.1. Comments

The first line in Welcome.java in Listing 1.1 is a comment that documents what the program is and how the program is constructed. Comments help programmers to communicate and understand the program. Comments are not programming statements and thus are ignored by the compiler. In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a paragraph comment. When the compiler sees //, it ignores all text after // on the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */.
Here are examples of the two types of comments:
// This application program prints Welcome to Java!
/* This application program prints Welcome to Java! */
/* This application program
   prints Welcome to Java! */

Note

In addition to the two comment styles, // and /*, Java supports comments of a special type, referred to as javadoc comments. javadoc comments begin with /** and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command. For more information, see java.sun.com/j2se/javadoc.


1.2. Reserved Words

Reserved words, or keywords, are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in Listing 1.1 are public, static, and void. Their use will be introduced later in the book.

Tip

Because Java is case-sensitive, public is a reserved word, but Public is not. Nonetheless, for clarity and readability, it would be best to avoid using reserved words in other forms. (See Appendix A, "Java Keywords.")


1.3. Modifiers

Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public datum, method, or class can be accessed by other classes. A private datum or method cannot be accessed by other classes.

1.4. Statements

A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!"); in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;).

1.5. Blocks

The braces in the program form a block that groups the components of the program. In Java, each block begins with an opening brace ({) and ends with a closing brace (}). Every class has a class block that groups the data and methods of the class. Every method has a method block that groups the statements in the method. Blocks can be nested, meaning that one block can be placed within another, as shown in the following code.



1.6. Classes

The class is the essential Java construct. To program in Java, you must understand classes and be able to write and use them. The mystery of classes will be unveiled throughout the book. For now, though, it is enough to know that a program is defined by using one or more classes.

1.7. Methods

What is System.out.println? System.out is known as the standard output object. println is a method in the object, which consists of a collection of statements that perform a sequence of operations to display a message to the standard output device. If you run the program from the command window, the output from the System.out.println is displayed in the command window. The method can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument. The string argument is enclosed in parentheses. In this case, the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message.

1.8. The main Method

Every Java application must have a user-declared main method that defines where the program execution begins. The JVM executes the application by invoking the main method. The main method looks like this:
public static void main(String[] args) {
  // Statements;
}

No comments:

Post a Comment