Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


Part VII
Java

CHAPTER 36
Introduction to Java

by Mike Morgan

In this chapter
What Is Java? 964
What’s 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 you’re writing for a Windows environment, for example, you might make reference to the Microsoft Foundation Classes. If your target machine is a Macintosh, you’ll 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 you’re 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 don’t need to think about calls to Windows, the Mac OS, or other operating system libraries. Java contains its own libraries—called packages—that are platform independent.

Similarly, you aren’t concerned with whether the finished product will run on an Intel Pentium, an IBM PowerPC, or a Sun SPARC processor. The Java compiler doesn’t generate native instructions. Instead, it writes bytecodes for a machine that doesn’t really exist—the Java Virtual Machine, or JVM.


NOTE:  The Java compiler generates files of bytecodes—instructions 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 doesn’t 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 you’ve 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 user’s machine that’s 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. We’ll 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 application’s 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
ON THE WEB
http://java.sun.com/ You can download a free copy of the latest Java JDK at this URL.


The Mac OS doesn’t 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, don’t 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 don’t 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.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.