To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98
Part VII Java
CHAPTER 36 Introduction to Java
by Mike Morgan
- In this chapter
- What Is Java? 964
- Whats New in Java? 970
What Is Java?
When you write in most programming languages, you need to decide which processor and operating system your finished program is intended to run on. Then you include specific function calls to a library associated with the operating system of that target platform. If youre writing for a Windows environment, for example, you might make reference to the Microsoft Foundation Classes. If your target machine is a Macintosh, youll call functions in the Mac OS Toolbox. Figure 36.1 illustrates this process.
FIGURE 36.1 In most programming languages, you make calls directly to the native operating system.
NOTE: In December 1998 Sun released Java 2 Platform. The actual name of the JDK that you download from their site, however, is JDK 1.2. We have elected to refer to the most recent version of Java as JDK 1.2 throughout this book.
When youre ready to test your program, you send your source code through a compiler that transforms your code into a set of native instructions for whatever processor your target machine uses. Windows is usually run on an Intel processor such as a Pentium, for example, and Macs use the Motorola 680x0 or PowerPC processors.
When you write Java, you dont need to think about calls to Windows, the Mac OS, or other operating system libraries. Java contains its own librariescalled packagesthat are platform independent.
Similarly, you arent concerned with whether the finished product will run on an Intel Pentium, an IBM PowerPC, or a Sun SPARC processor. The Java compiler doesnt generate native instructions. Instead, it writes bytecodes for a machine that doesnt really existthe Java Virtual Machine, or JVM.
NOTE: The Java compiler generates files of bytecodesinstructions for the Java Virtual Machine (JVM). Because the JVM has been ported to nearly every kind of computer, these files of bytecodes will serve as cross-platform applications.
Because the JVM doesnt really exist in the physical sense, how does the Java code run? Sun (and others) have implemented a software version of the JVM for most common platforms. When you load the file of bytecodes (called a class file) onto the target machine, it runs on the JVM on that machine. The JVM reads the class file and does the work specified by the original Java. Figure 36.2 illustrates how the Java compiler, the class file, and the JVM interact.
FIGURE 36.2 The output of the Java compiler is interpreted by the JVM on each specific platform.
Because the JVM is easy to port from one machine to another, you can expect that any new processor or operating system will soon have an implementation of the JVM. After youve written Java code that runs on one machine, you can run it on any common platform.
NOTE: The JVM is part of a larger collection of software on the end users machine thats called the Java Runtime Environment, or JRE. Browser vendors such as Microsoft and Netscape include a JRE in their Web browsers. If you want end users to be able to run Java applications, you need to make sure they have a JRE. You get a JRE in your Java Development Kit; end users can download the JRE separately.
Understanding Applications
If all you could do with Java was write portable applications, it would still be an important development. In 1993, however, Sun noticed that the Internet was gaining in popularity, and it began to position Java to run inside Web browsers. Standalone Java programs are known as applications, and programs that run with the help of a Web browser are known a apllets. Well talk talk about applications in this section and get to applets later in this chapter.
In most languages the finished product is an executable file of native binary instructions. In the DOS and Windows environments, we can recognize these files because they have the suffix .exe. In a graphical user environment such as Mac OS or Windows, we can double-click the applications icon to run the program.
Java is a little different. Because the class files contain bytecodes for the JVM, we have to launch an implementation of the JVM in order to run the application. The Java Development Kit (JDK) includes a Java interpreter called java that implements the JVM. To run an application named myApp from the JVM, you go to the command prompt and type
java myApp
|
| The Mac OS doesnt have a command prompt. To run an application on the Mac, drag the icon of the class file onto the icon of the Java interpreter.
|
|
|
| If you want to deliver an application to an end user, dont ask them to go to the command prompt. Write a batch file that launches the Java interpreter and starts the application. Have the end user double-click the batch file, the same as they would any other application.
|
|
Who Needs Applets?
Modern Web browsers such as Netscape Navigator and Microsoft Internet Explorer are highly capable programs with a rich set of features. Why would anyone need to extend the browser through applets?
Many Web designers want to go beyond simple displays of static contents. They want dynamic or live pages that are able to interact with the user. Often the best way to add dynamic content is to write a program, yet the Hypertext Markup Language (HTML) that is used to write Web pages has no programming capability at all.
Both the Netscape and Microsoft browsers support scripting languages such as JavaScript. Those languages enable you to attach functions to HTML elements such buttons, but you dont have complete control over the appearance of the user interface. You also cannot use these scripting languages to connect the client machine back to the network, so you cannot write true client-server programs. Sometimes you need a solution that is more powerful than these scripting languages, or you need a solution that does not depend on a particular browser. For those times, a Java applet is ideal.
You place a Java applet on your Web page by using the HTML <APPLET> tag. Because Java runs on any popular platform, the applet will appear and will work as expected as long as the visitor to the site is using one of the Java-capable browsers.
|