-->
Previous Table of Contents Next


Chapter 54
Java and JavaScript Basics

by Tim Parker

In This Chapter
•   What you need
•   The Java language
•   JavaScript and HTML

A quick word before we start: we’re not going to teach you how to program Java in this chapter! There’s far too much material to do justice in a few pages. Instead, we’ll look at what Java is and does, and some of the basic programming aspects.

What is Java? Java is a programming language developed at Sun Microsystems. Sun describes Java in their press releases as “a simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language.” What does all that really mean? To start with, Java was intended to be a much simpler object-oriented language to work with than C++ or SmallTalk, both of which are large and cumbersome languages to work with. By producing a small object-oriented language, Sun’s developers also made Java simple and much less prone to bugs than larger languages. That’s the simple and robust aspects of the language. The small size of the Java language also contributes to performance.

Java is an interpretive language, meaning that each line of source code is read by the Java interpreter and executed on that basis instead of a compiled executable. Actually, that’s a bit of a simplification because Java code is pseudo-compiled to produce a binary object file, called a class file. This approach may be slower than a true compiled system, but by using a platform-neutral language (meaning there are no hardware or operating system-specific instructions in the language) Java source code will execute on any system with a Java interpreter. That covers the architecture-neutral and portable aspects of Sun’s description. The distributed aspect comes naturally from these points, because Java source code can be easily sent from one machine to another across a network for execution. This allows a server to send Java code to clients, making a distributed system (Java runs on the client and communicates back to the server).

Because Java can run on practically any operating system, it can take advantage of the host operating system’s features, such as UNIX’s capability to handle multithreading. Java by itself can be thought of as multithreaded, but the operating system contributes a lot in this regard. Finally, the security aspect of Java was one of the design principles of the development group. A secure method of transferring information from client to server and vice versa was needed, and Java was designed to fill this requirement.

To provide the client and server software components, Java is designed to have the interpretive component of the language attached to other software, most commonly a Web browser. Netscape’s Navigator and Microsoft’s Explorer, for example, both have the client components of Java attached (or “plugged in” in Web parlance) to the browser code. When incoming Java source code is detected, the Java interpreter starts up and handles the task.

JavaScript was introduced after Java was on the market for a while. JavaScript is built into most Java-enabled Web browsers. Aside from their names, JavaScript and Java don’t share very much. Many people think of JavaScript as a stripped-down Java, and that is incorrect and misleading. JavaScript is more an extension of HTML that allows users to build interactive Web pages in a client-server system.

JavaScript has a number of uses that make it attractive, including the capability to tell what a user is doing. When a user leaves a page or clicks on a certain button, the JavaScript client can communicate this information and start new routines. JavaScript is also ideal for writing little housekeeping tasks and for managing complex tasks, like string handling that is beyond HTML.

What You Need

To write Java applications you need the Java Development Kit (JDK). The JDK contains all the software necessary to write, compile, and test Java applets. Besides the JDK, all you need is a Java-enabled browser to test and display your applets. Netscape Navigator and Microsoft Explorer’s latest releases all support Java, as do many other browsers. Sun developed their own Java-enabled browser called HotJava, which is available from the Sun Web site.


Tip:  
You can get the Java JDK at many sites on the Web or through FTP. The primary location is the Sun page http://java.sun.com, although most Linux sites (such as www.blackdown.org) also contain pointers to mirrors or backup sites for the JDK. For a Java-enabled Web browser, check out both Netscape and Microsoft home pages, as well as Sun’s HotJava. For other browsers and Java development tools, check the Linux sites on the Web.

For FTP access to the JDK, FTP to java.sun.com and change to the directory /pub, which contains the files you will need. If you expect Sun to support JDK under Linux, you’ll be disappointed because they refuse to offer any kind of help.


The Sun Java section also contains a wealth of details on Java development, lots of sample code, advice, and FAQs. A white paper on Java obtained at the same site (http://java.sun.com/tutorial/java/index.html) is an excellent introduction to the language. The Java Development Kit is free if you are using it for personal reasons only, but if you plan to publish Java-based Web pages, you may need a license. Details are included with the JDK.

When installing the JDK make sure the path to the Java executables is in your path. Even better, link the executables to /usr/bin. The most common errors Java programmers make while learning the language are confusion about case sensitivity and class use. Java, like UNIX, is case sensitive, so developers must be careful to ensure their code is correct. Class usage follows C++ methods and must be adhered to properly. A good Java book is a must for learning the language, and there are dozens of books on Java.

For JavaScript, all you need is a JavaScript-enabled browser (such as Netscape Navigator 3.0 or higher), a standard ASCII editor or an editor that can save in ASCII, and a TCP/IP stack to communicate with other machines over the Internet or an intranet.


Previous Table of Contents Next