1.11. Displaying Text in a Message Dialog Box

 
[Page 20 ( continued )]

1.10. 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.10.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! */  


[Page 21]

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.10.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.10.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.10.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.10.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.10.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.


[Page 22]

1.10.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.10.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;  } 

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net