1.1. What Is Java?
In discussing Java, it is important to
distinguish between the Java programming language, the Java Virtual
Machine, and the Java platform. The Java programming language is the
language in which Java applications, applets, servlets, and
components are written. When a Java program is compiled, it is
converted to byte codes that are the portable machine language of a
CPU architecture known as the Java Virtual Machine (also called the
Java VM or JVM). The JVM can be implemented directly in hardware, but
it is usually implemented in the form of a software program that
interprets and executes byte codes.The
Java platform is distinct from both the Java language and Java VM.
The Java platform is the predefined set of Java classes that exist on
every Java installation; these classes are available for use by all
Java programs. The Java platform is also sometimes referred to as the
Java runtime environment or the core Java APIs (application
programming interfaces). The Java platform can be extended with
optional packages (formerly called standard extensions). These APIs
exist in some Java installations but are not guaranteed to exist in
all installations.
1.1.1. The Java Programming Language
The Java programming language is a
state-of-the-art, object-oriented language that has a syntax similar
to that of C. The language designers strove to make the Java language
powerful, but, at the same time, they tried to avoid the overly
complex features that have bogged down other object-oriented
languages like C++. By keeping the language simple, the designers
also made it easier for programmers to write robust, bug-free code.
As a result of its elegant design and next-generation features, the
Java language has proved popular with programmers, who typically find
it a pleasure to work with Java after struggling with more difficult,
less powerful languages.Java 5.0, the latest version of the Java language,[1] includes a number of new language features, most notably
generic types, which increase both the complexity and the power of
the language. Most experienced Java programmers have welcomed the new
features, despite the added complexity they bring.
[1] Java 5.0 represents a significant change in version numbering
for Sun. The previous version of Java is Java 1.4 so you may
sometimes hear Java 5.0 informally referred to as Java 1.5.
1.1.2. The Java Virtual Machine
The
Java Virtual Machine, or Java interpreter, is the crucial piece of
every Java installation. By design, Java programs are portable, but
they are only portable to platforms to which a Java interpreter has
been ported. Sun ships VM implementations for its own Solaris
operating system and for Microsoft Windows and Linux platforms. Many
other vendors, including Apple and various commercial Unix vendors,
provide Java interpreters for their platforms. The Java VM is not
only for desktop systems, however. It has been ported to set-top
boxes and handheld devices that run Windows CE and PalmOS.Although interpreters are not typically
considered high-performance systems, Java VM performance has improved
dramatically since the first versions of the language. The latest
releases of Java run remarkably fast. Of particular note is a VM
technology called just-in-time (JIT) compilation
whereby Java byte codes are converted on the fly into native platform
machine language, boosting execution speed for code that is run
repeatedly.
1.1.3. The Java Platform
The Java platform is just as important
as the Java programming language and the Java Virtual Machine. All
programs written in the Java language rely on the set of predefined
classes[2] that comprise the Java platform. Java
classes are organized into related groups known as
packages . The Java platform defines packages for
functionality such as input/output, networking, graphics,
user-interface creation, security, and much more.
[2] A class is a module of
Java code that defines a data structure and a set of methods (also
called procedures, functions, or subroutines) that operate on that
data.
It is important to understand what is
meant by the term platform. To a computer programmer, a platform is
defined by the APIs he can rely on when writing programs. These APIs
are usually defined by the operating system of the target computer.
Thus, a programmer writing a program to run under Microsoft Windows
must use a different set of APIs than a programmer writing the same
program for a Unix-based system. In this respect, Windows and Unix
are distinct platforms.Java is not an operating system. Nevertheless, the Java platform
provides APIs with a comparable breadth and depth to those defined by
an operating system. With the Java platform, you can write
applications in Java without sacrificing the advanced features
available to programmers writing native applications targeted at a
particular underlying operating system. An application written on the
Java platform runs on any operating system that supports the Java
platform. This means you do not have to create distinct Windows,
Macintosh, and Unix versions of your programs, for example. A single
Java program runs on all these operating systems, which explains why
"Write once, run anywhere" is
Sun's motto for Java.The Java platform is not an operating system, but for programmers, it
is an alternative development target and a very popular one at that.
The Java platform reduces programmers' reliance on
the underlying operating system, and, by allowing programs to run on
top of any operating system, it increases end users'
freedom to choose an operating system.
1.1.4. Versions of Java
As
of this writing, there have been six major versions of Java. They
are:
- Java 1.0
This was the first public version of Java. It contained 212 classes
organized in 8 packages. It was simple and elegant but is now
completely outdated.- Java 1.1
This release of Java more than doubled the size of the Java platform
to 504 classes in 23 packages. It introduced nested types (or
"inner classes"), an important
change to the Java language itself, and included significant
performance improvements in the Java VM. This version is outdated.- Java 1.2
This was a very significant release of Java; it tripled the size of
the Java platform to 1,520 classes in 59 packages. Important
additions included the Collections API for working with sets, lists,
and maps of objects and the Swing API for creating graphical user
interfaces. Because of the many new features included in the 1.2
release, the platform was rebranded as "the Java 2
Platform." The term "Java
2" was simply a trademark, however, and not an
actual version number for the release.- Java 1.3
This was primarily a maintenance release, focused on bug fixes,
stability, and performance improvements (including the
high-performance "HotSpot" virtual
machine). Additions to the platform included the Java Naming and
Directory Interface (JNDI) and the Java Sound APIs, which were
previously available as extensions to the platform. The most
interesting classes in this release were probably
java.util.Timer and
java.lang.reflect.Proxy. In total, Java 1.3
contains 1,842 classes in 76 packages.- Java 1.4
This was another big release, adding important new functionality and
increasing the size of the platform by 62% to 2,991 classes and
interfaces in 135 packages. New features included a high-performance,
low-level I/O API; support for pattern matching with regular
expressions; a logging API; a user preferences API; new Collections
classes; an XML-based persistence mechanism for JavaBeans; support
for XML parsing using both the DOM and SAX APIs; user authentication
with the Java Authentication and Authorization Service (JAAS) API;
support for secure network connections using the SSL protocol;
support for cryptography; a new API for reading and writing image
files; an API for network printing; a handful of new GUI components
in the Swing API; and a simplified drag-and-drop architecture for
Swing. In addition to these platform changes, the 1.4 release
introduced an assert statement to the Java
language.- Java 5.0
The most recent release of Java introduces a number of changes to the
core language itself including generic types, enumerated types,
annotations, varargs methods, autoboxing, and a new
for/in statement. Because of the major language
changes, the version number was incremented. This release would
logically be known as "Java 2.0" if
Sun had not already used the term "Java
2" for marketing Java 1.2.In addition to the language changes, Java 5.0 includes a number of
additions to the Java platform as well. This release includes 3562
classes and interfaces in 166 packages. Notable additions include
utilities for concurrent programming, a remote management framework,
and classes for the remote management and instrumentation of the Java
VM itself.See the Preface for a list of changes in this edition of the book,
including pointers to coverage of the new language and platform
features.
To write programs in Java, you
must obtain the Java Development Kit ( JDK). Sun releases a new
version of the JDK for each new version of Java.
Don't confuse the JDK with the Java Runtime
Environment ( JRE). The JRE contains everything you need to run Java
programs, but it does not contain the tools you need to develop Java
programs (primarily the compiler).In addition to the Standard Edition of
Java used by most Java developers and documented in this book, Sun
has also released the Java 2 Platform, Enterprise Edition (or J2EE)
for enterprise developers and the Java 2 Platform, Micro Edition
(J2ME) for consumer electronic systems, such as handheld PDAs and
cellular telephones. See Java Enterprise in a
Nutshell and Java Micro Edition in a
Nutshell (both by O'Reilly) for more
information on these other editions.