Read an Excerpt
Chapter 1: What is Java
This chapter gives you the 10-minute tour of Java. if you're already experienced with Java, you might want to skip ahead. On the other hand, if you're new here, you might find that Java is not exactly what you thought it was. It's not just a language, and it's not J . ust for Web browsers.So what exactly is Java? It's a language. It's a machine architecture. It's a loading model. It's a file format. it's an applications environment (several different applications environments, actually). It's a specification. And it's an implementation.
Java began life as a failed Sun research project called Oak, targeted at embedded operation in appliances. In 1995, Sun repackaged Oak as a portable "Internet programming language" and positioned it initially as a way to run programs in Web browsers. The result was something of a misfire: Web applets were not a huge success, and even today they occupy a largely specialized niche. But Java displayed usefulness in other areas, and interest in Java for different tasks-particularly Web services and enterprise connectivity-skyrocketed. Java has since settled into a number of important application areas (we explore more below), including, at long last, appliances!
The Many Faces of Java
Let's dissect Java in a bit more detail...
The Java Language
By the time Sun announced Java in 1995, C++ and object-oriented programming had been around for years. C++ had grown, in episodic spurts, from a preprocessor into a full-featured compiled language. It had become the language of choice for projects of all scales, and it had been through several stages ofstandardization-culminating in the acceptance of the ANSI C++ standard in 1998.
C++ had also, along the way picked up considerable baggage. It had a substantial number of non-object-oriented artifacts, and it had become a difficult language to write compilers for. It was also difficult to achieve complete portability: even the excellent ANSI standardization did not completely shield developers from platform-specific language porting headaches.
One of Java's goals was to fix what was wrong with C++, with a special focus on the errorprone aspects of C++ development-those that tend to take up too much debugging time. In this it has certainly succeeded: Java developers (especially C++ converts) find the language well-suited for rapid prototyping and development. Java's remedies include:
- Strengthening the object orientation and eliminating non-object-oriented features (for example, macros, globals)
- Eliminating the error-prone direct manipulation of memory pointers and the confusion of referencing and dereferencing
- Getting the developer out of the messy memory management business
- Adding type safety
- Performing runtime checking for such common problems as illegal typecasting, bad array subscripts, and null object references
- Supporting multithreaded programming directly in the language
- Improving exception handling
Chapter 2, "Moving from C++ to Java," uses some programming examples to take a closer look at the differences between Java and C++. Despite the differences, Java looks much like C++, and experience suggests that C++ programmers can pick it up quickly and easily So although this book is not in the business of teaching the language, the introduction and the examples should be enough to get you well past the "Hello World" stage.
The Java Machine Architecture
The Java specification describes not only the high-level language but also the low-level machine and instruction set it runs on: a concurrent, abstract stack machine with an architecture and a small bytecode instruction set closely tied to the language (see Figure 1. 1). This is roughly equivalent to dictating the CPU on which a language can be used, although a better analog is the P-code machine that was used in the development of UCSD Pascal some 20 years back.
implementation of the architecture-as, for example, a silicon Java chip or as a virtual machine-is left as an exercise for individual vendors. (This has turned out to be a challenge to the acceptance of Java, but virtual machines are now available for Linux and many other environments.)
In addition to describing an execution engine, the spec describes certain machine behaviors: startup, shutdown, and, most interestingly, loading.
The Java Loading Model
The Java loading model is, ultimately, what makes Java unique. Loading of Java modules (or classes, to be more correct) happens dynamically during program execution. This is a radical change for generations of programmers accustomed to the compile-link-load-run cycle for building and running programs and is resulting in new approaches to structuring application functionality
The loading of Java classes consists of several steps (see Figure 1.2):
1 . Reading the bits
2. Verifying that the bits describe a well-structured class containing well-structured Java code
3. Building a global class structure
4. Resolving references
5. Controlling access-allowing an application or environment to decide access rules for class loading (such as restriction to trusted sources)...