1.9. Creating, Compiling, and Executing a Java Program

 
[Page 17 ( continued )]

1.8. A Simple Java Program

A Java program can be written in many ways. This book introduces Java applications, applets, and servlets. Applications are standalone programs that can be executed from any computer with a JVM. Applets are special kinds of Java programs that run from a Web browser. Servlets are special kinds of Java programs that run from a Web server to generate dynamic Web contents. Applets will be introduced in Chapter 16, and servlets will be introduced in Chapter 34.

Let us begin with a simple Java program that displays the message "Welcome to Java!" on the console. The program is shown in Listing 1.1.

Listing 1.1. Welcome.java


Every Java program must have at least one class. A class is a construct that defines data and methods . Each class has a name . By convention, class names start with an uppercase letter. In this example, the class name is Welcome .

In order to run a class, the class must contain a method named main . The JVM executes the program by invoking the main method .

A method is a construct that contains statements. The main method in this program contains the System.out.println statement. This statement prints a message "Welcome to Java!" to the console.


[Page 18]

Note

You are probably wondering about such points as why the main method is declared this way and why System.out.println(...) is used to display a message to the console. Your questions cannot be fully answered yet. For the time being, you will just have to accept that this is how things are done. You will find the answers in the coming chapters.


Note

Like any other programming language, Java has its own syntax, and you need to write code that obeys the syntax rules . The Java compiler will report syntax errors if your program violates the syntax rules.


 


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