17-05-2013, 02:49 PM
Teach Yourself JAVA in 21 Days
Teach Yourself.pdf (Size: 5.76 MB / Downloads: 233)
Introduction
The World Wide Web, for much of its existence, has been a method for distributing passive
information to a widely distributed number of people. The Web has, indeed, been exceptionally
good for that purpose. With the addition of forms and image maps, Web pages began to become
interactive—but the interaction was often simply a new way to get at the same information. The
limitations of Web distribution were all too apparent once designers began to try to stretch the
boundaries of what the Web can do. Even other innovations, such as Netscape’s server push to
create dynamic animations, were merely clever tricks layered on top of a framework that wasn’t
built to support much other than static documents with images and text.
Enter Java, and the capability for Web pages of containing Java applets. Applets are small
programs that create animations, multimedia presentations, real-time (video) games, multi-user
networked games, and real interactivity—in fact, most anything a small program can do, Java
applets can. Downloaded over the net and executed inside a Web page by a browser that supports
Java, applets are an enormous step beyond standard Web design.
The disadvantage of Java is that to create Java applets right now, you need to write them in the
Java language. Java is a programming language, and as such, creating Java applets is more
difficult than creating a Web page or a form using HTML. Soon there will be tools and programs
that will make creating Java applets easier—they may be available by the time you read this. For
now, however, the only way to delve into Java is to learn the language and start playing with the
raw Java code. Even when the tools come out, you may want to do more with Java than the tools
can provide, and you’re back to learning the language.
How This Book Is Organized
Teach Yourself Java in 21 Days describes Java primarily in its current state—what’s known as the
beta API (Application Programming Interface). This is the version of Java that Netscape and
other browsers, such as Spyglass’s Mosaic, support. A previous version of Java, the alpha API,
was significantly different from the version described in this book, and the two versions are not
compatible with each other. There are other books that describe only the alpha API, and there
may still be programs and browsers out there that can only run using alpha Java programs.
Teach Yourself Java in 21 Days uses primarily Java beta because that is the version that is most
current and is the version that will continue to be used in the future. The alpha API is obsolete
and will eventually die out. If you learn Java using beta API, you’ll be much better prepared for
any future changes (which will be minor) than if you have to worry about both APIs at once.
Java is still in development. “Beta” means that Java is not complete and that things may change
between the time this book is being written and the time you read this. Keep this in mind as you
work with Java and with the software you’ll use to create and compile programs. If things aren’t
behaving the way you expect, check the Web sites mentioned at the end of this introduction for
more information.
Web Sites for Further Information
Before, while, and after you read this book, there are two Web sites that may be of interest to
you as a Java developer.
The official Java web site is at http://java.sun. At this site, you’ll find the Java
development software, the HotJava web browser, and online documentation for all aspects of
the Java language. It has several mirror sites that it lists online, and you should probably use the
site “closest” to you on the Internet for your downloading and Java Web browsing.
What Is Java?
Java is an object-oriented programming language developed by Sun Microsystems, a company
best known for its high-end Unix workstations. Modeled after C++, the Java language was
designed to be small, simple, and portable across platforms and operating systems, both at the
source and at the binary level (more about this later).
Java is often mentioned in the same breath as HotJava, a World Wide Web browser from Sun
like Netscape or Mosaic (see Figure 1.1). What makes HotJava different from most other
browsers is that, in addition to all its basic Web features, it can also download and play applets
on the reader’s system. Applets appear in a Web page much in the same way as images do, but
unlike images, applets are dynamic and interactive. Applets can be used to create animations,
figures, or areas that can respond to input from the reader, games, or other interactive effects on
the same Web pages among the text and graphics.
Although HotJava was the first World Wide Web browser to be able to play Java applets, Java
support is rapidly becoming available in other browsers. Netscape 2.0 provides support for Java
applets, and other browser developers have also announced support for Java in forthcoming
products.
Java’s Past, Present, and Future
The Java language was developed at Sun Microsystems in 1991 as part of a research project to
develop software for consumer electronics devices—television sets, VCRs, toasters, and the
other sorts of machines you can buy at any department store. Java’s goals at that time were to
be small, fast, efficient, and easily portable to a wide range of hardware devices. It is those same
goals that made Java an ideal language for distributing executable programs via the World Wide
Web, and also a general-purpose programming language for developing programs that are easily
usable and portable across different platforms.
The Java language was used in several projects within Sun, but did not get very much commercial
attention until it was paired with HotJava. HotJava was written in 1994 in a matter of months,
both as a vehicle for downloading and running applets and also as an example of the sort of
complex application that can be written in Java.
At the time this book is being written, Sun has released the beta version of the Java Developer’s
Kit (JDK), which includes tools for developing Java applets and applications on Sun systems
running Solaris 2.3 or higher for Windows NT and for Windows 95. By the time you read this,
support for Java development may have appeared on other platforms, either from Sun or from
third-party companies.
Why Learn Java?
At the moment, probably the most compelling reason to learn Java—and probably the reason
you bought this book—is that HotJava applets are written in Java. Even if that were not the case,
Java as a language has significant advantages over other languages and other programming
environments that make it suitable for just about any programming task. This section describes
some of those advantages.
Java Is Object-Oriented
To some, object-oriented programming (OOP) technique is merely a way of organizing
programs, and it can be accomplished using any language. Working with a real object-oriented
language and programming environment, however, enables you to take full advantage of objectoriented
methodology and its capabilities of creating flexible, modular programs and reusing
code.
Many of Java’s object-oriented concepts are inherited from C++, the language on which it is
based, but it borrows many concepts from other object-oriented languages as well. Like most
object-oriented programming languages, Java includes a set of class libraries that provide basic
data types, system input and output capabilities, and other utility functions. These basic classes
are part of the Java development kit, which also has classes to support networking, common
Internet protocols, and user interface toolkit functions. Because these class libraries are written
in Java, they are portable across platforms as all Java applications are.
Getting Started with
Programming in Java
Enough background! Let’s finish off this day by creating two real Java programs: a stand-alone
Java application and an applet that you can view in either in the appletviewer (part of the JDK)
or in a Java-capable browser. Although both these programs are extremely simple, they will give
you an idea of what a Java program looks like and how to compile and run it.
Applets and Applications
Java applications fall into two main groups: applets and applications.
Applets, as you have learned, are Java programs that are downloaded over the World Wide Web
and executed by a Web browser on the reader’s machine. Applets depend on a Java-capable
browser in order to run (although they can also be viewed using a tool called the appletviewer,
which you’ll learn about later today).
Java applications are more general programs written in the Java language. Java applications don’t
require a browser to run, and in fact, Java can be used to create most other kinds of applications
that you would normally use a more conventional programming language to create. HotJava
itself is a Java application.