Thursday, November 10, 2011

JAVA (I)

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

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.

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
You must first install and configure JDK before compiling and running programs. See Supplement I.B, "Installing and Configuring JDK 5.0," on how to install JDK and how to set up the environment to compile and run Java programs. If you have trouble compiling and running programs, please see Supplement I.C, "Compiling and Running Java from the Command Window." This supplement also explains how to use basic DOS commands and how to use Windows NotePad and WordPad to create and edit files. All the supplements are accessible from the Companion Website.


Caution

Java source programs are case-sensitive. It would be wrong, for example, to replace main in the program with Main. Program filenames are case-sensitive on UNIX but generally not on Windows; JDK treats filenames as case-sensitive on any platform. If you try to compile the program using javac welcome.java, you will get a file-not-found error.

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

Do not use the extension .class in the command line when executing the program. The JVM assumes that the first argument in the command is the filename and then fetches filename.class to execute. It would attempt to fetch filename.class.class if you used java filename.class in the command line.


Tip

If you execute a class file that does not exist, a NoClassDefFoundError exception will occur. If you execute a class file that does not have a main method or you mistype the main method (e.g., by typing Main instead of main), a NoSuchMethodError will occur.


Note

When executing a Java program, the JVM first loads the bytecode of the class to memory using a program called the class loader. If your program uses other classes, the class loader dynamically loads them just before they are needed. After a class is loaded, the JVM uses a program called bytecode verifier to check the validity of the bytecode and ensure that the bytecode does not violate Java's security restrictions. Java enforces strict security to make sure that Java programs arriving from the network do not harm your computer

No comments:

Post a Comment