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.
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.
Note
Note
Creating, Compiling, and Executing a Java Program
You have to create your program and
compile it before it can be executed. This process is repetitive, as shown in Figure 1.11. If your
program has compilation errors, you have to fix them by modifying the program,
then recompile it. If your program has runtime errors or does not produce the
correct result, you have to modify the program, recompile it, and execute it
again.
Figure 1.11. The Java programming-development process consists of creating/modifying source code, compiling, and executing programs.
You can use any text editor to create and edit a Java source code file, or you can use an IDE like Eclipse, JBuilder or
NetBeans. Figure 1.12 shows how to use NotePad to create and edit the source code
file.
Figure 1.12. You can create the Java source file using Windows NotePad.
This file must end with the extension .java and must have
the exact same name as the public class name. For example, the file for the
source code in Listing 1.1 should be named Welcome.java, since the public class name
is Welcome.
A Java compiler translates a Java
source file into a Java bytecode file. The following command compiles Welcome.java:
javac Welcome.java
Note
Caution
If there are no syntax errors, the compiler generates a
bytecode file with a .class extension. So the preceding command generates a file
named Welcome.class. The bytecode is similar to machine
instructions but is architecture-neutral and can run on any platform that has a
JVM. This is one of Java's primary advantages: Java bytecode can run on a variety
of hardware platforms and operating systems.
To execute a Java program is to run the program's bytecode. You
can execute the bytecode on any platform with a JVM. The following command runs
the bytecode:
java Welcome
Figure 1.13 shows the javac command for compiling Welcome.java. The
compiler generated the Welcome.class file. This file is executed using the java command.
Figure 1.13. The output of Listing 1.1 displays the message Welcome to Java!
Caution
Tip
Note
No comments:
Post a Comment