OReilly - Learning Java.pdf

(3718 KB) Pobierz
Microsoft Word - Learning Java -- O'REILLY.doc
22973666.001.png 22973666.002.png
O’Reilly Learning Java
Preface
This book is about the Java™ language and programming environment. If you've been at all active
on the Internet in the past few years, you've heard a lot about Java. It's one of the most exciting
developments in the history of the Internet, rivaling the creation of the World Wide Web. Java
became the darling of the Internet programming community as soon as the alpha version was
released. Immediately, thousands of people were writing Java applets to add to their web pages.
Interest in Java only grew with time, and support for Java in Netscape Navigator guaranteed it
would be a permanent part of the Net scene.
What, then, is Java? Java is a network programming language that was developed by Sun
Microsystems. It's already in widespread use for creating animated and interactive web pages.
However, this is only the start. The Java language and environment are rich enough to support
entirely new kinds of applications, like dynamically extensible browsers and mobile agents. There
are entirely new kinds of computer platforms being developed around Java (handheld devices and
network computers) that download all their software over the network. In the coming years, we'll
see what Java is capable of doing; fancy web pages are fun and interesting, but they certainly aren't
the end of the story. If Java is successful (and that isn't a foregone conclusion), it could change the
way we think about computing in fundamental ways.
This book gives you a head start on a lot of Java fundamentals. Learning Java attempts to live up to
its name by mapping out the Java language, its class libraries, programming techniques, and idioms.
We'll dig deep into interesting areas and at least scratch the surface of the rest. Other titles in the
O'Reilly & Associates Java Series will pick up where we leave off and provide more comprehensive
information on specific areas and applications of Java.
Whenever possible, we'll provide meaningful, realistic examples and avoid cataloging features. The
examples are simple but hint at what can be done. We won't be developing the next great "killer
app" in these pages, but we hope to give you a starting point for many hours of experimentation and
tinkering that will lead you to learn more on your own.
Learning Java
New Developments
Audience
Using This Book
Getting Wired
Conventions Used in This Book
How to Contact Us
Acknowledgments
1. Yet Another Language? - 4
1.1 Enter Java
1.2 A Virtual Machine
1.3 Java Compared with Other Languages
1.4 Safety of Design
1.5 Safety of Implementation
1.6 Application and User-Level Security
1.7 Java and the World Wide Web
1.8 Java as a General Application Language
1.9 A Java Road Map
2. A First Application - 25
2.1 HelloJava1
2.2 HelloJava2: The Sequel
- 2 -
Preface - 3
O’Reilly Learning Java
2.3 HelloJava3: The Button Strikes!
2.4 HelloJava4: Netscape's Revenge
3.1 The Java Interpreter
3.2 Policy Files
3.3 The Class Path
3.4 The Java Compiler
3.5 Java Archive (JAR) Files
4. The Java Language - 65
4.1 Text Encoding
4.2 Comments
4.3 Types
4.4 Statements and Expressions
4.5 Exceptions
4.6 Arrays
5. Objects in Java - 92
5.1 Classes
5.2 Methods
5.3 Object Creation
5.4 Object Destruction
6.1 Subclassing and Inheritance
6.2 Interfaces
6.3 Packages and Compilation Units
6.4 Visibility of Variables and Methods
6.5 Arrays and the Class Hierarchy
6.6 Inner Classes
7. Working with Objects and Classes - 136
7.1 The Object Class
7.2 The Class Class
7.3 Reflection
8.1 Introducing Threads
8.2 Threads in Applets
8.3 Synchronization
8.4 Scheduling and Priority
8.5 Thread Groups
9. Basic Utility Classes - 170
9.1 Strings
9.2 Math Utilities
9.3 Dates
9.4 Timers
9.5 Collections
Preface
This book is about the Java™ language and programming environment. If you've been at all active
on the Internet in the past few years, you've heard a lot about Java. It's one of the most exciting
developments in the history of the Internet, rivaling the creation of the World Wide Web. Java
became the darling of the Internet programming community as soon as the alpha version was
released. Immediately, thousands of people were writing Java applets to add to their web pages.
Interest in Java only grew with time, and support for Java in Netscape Navigator guaranteed it
would be a permanent part of the Net scene.
- 3 -
3. Tools of the Trade - 53
6. Relationships Among Classes - 108
8. Threads - 149
O’Reilly Learning Java
What, then, is Java? Java is a network programming language that was developed by Sun
Microsystems. It's already in widespread use for creating animated and interactive web pages.
However, this is only the start. The Java language and environment are rich enough to support
entirely new kinds of applications, like dynamically extensible browsers and mobile agents. There
are entirely new kinds of computer platforms being developed around Java (handheld devices and
network computers) that download all their software over the network. In the coming years, we'll
see what Java is capable of doing; fancy web pages are fun and interesting, but they certainly aren't
the end of the story. If Java is successful (and that isn't a foregone conclusion), it could change the
way we think about computing in fundamental ways.
This book gives you a head start on a lot of Java fundamentals. Learning Java attempts to live up to
its name by mapping out the Java language, its class libraries, programming techniques, and idioms.
We'll dig deep into interesting areas and at least scratch the surface of the rest. Other titles in the
O'Reilly & Associates Java Series will pick up where we leave off and provide more comprehensive
information on specific areas and applications of Java.
Whenever possible, we'll provide meaningful, realistic examples and avoid cataloging features. The
examples are simple but hint at what can be done. We won't be developing the next great "killer
app" in these pages, but we hope to give you a starting point for many hours of experimentation and
tinkering that will lead you to learn more on your own.
Chapter 1. Yet Another Language?
The greatest challenges and most exciting opportunities for software developers today lie in
harnessing the power of networks. Applications created today, whatever their intended scope or
audience, will almost certainly be run on machines linked by a global network of computing
resources. The increasing importance of networks is placing new demands on existing tools and
fueling the demand for a rapidly growing list of completely new kinds of applications.
We want software that works—consistently, anywhere, on any platform—and that plays well with
other applications. We want dynamic applications that take advantage of a connected world,
capable of accessing disparate and distributed information sources. We want truly distributed
software that can be extended and upgraded seamlessly. We want intelligent applications—like
autonomous agents that can roam the Net for us, ferreting out information and serving as electronic
emissaries. We know, to some extent, what we want. So why don't we have it?
The problem has been that the tools for building these applications have fallen short. The
requirements of speed and portability have been, for the most part, mutually exclusive, and security
has been largely ignored or misunderstood. There are truly portable languages, but they are mostly
bulky, interpreted, and slow. These languages are popular as much for their high-level functionality
as for their portability. And there are fast languages, but they usually provide speed by binding
themselves to particular platforms, so they can meet the portability issue only halfway. There are
even a few recent safe languages, but they are primarily offshoots of the portable languages and
suffer from the same problems.
1.1 Enter Java
The Java™ programming language, developed at Sun Microsystems under the guidance of Net
luminaries James Gosling and Bill Joy, is designed to be a machine-independent programming
language that is both safe enough to traverse networks and powerful enough to replace native
- 4 -
O’Reilly Learning Java
executable code. Java addresses the issues raised here and may help us start building the kinds of
applications we want.
Initially, most of the enthusiasm for Java centered around its capabilities for building embedded
applications for the World Wide Web; these applications are called applets . Applets could be
independent programs in themselves, or sophisticated frontends to programs running on a server.
More recently, interest has shifted to other areas. With Java 2, Java has the most sophisticated
toolkit for building graphical user interfaces; this development has allowed Java to become a
popular platform for developing traditional application software. Java has also become an important
platform for server-side applications, using the servlet interface, and for enterprise applications
using technologies like Enterprise JavaBeans™. And Java is the platform of choice for modern
distributed applications.
This book shows you how to use Java to accomplish real programming tasks, such as building
networked applications and creating functional user interfaces. There's still a chapter devoted to
applets; they may become more important again when the Java 2 (and subsequent) versions of the
Java platform are more widely distributed in web browsers.
1.1.1 Java's Origins
The seeds of Java were planted in 1990 by Sun Microsystems patriarch and chief researcher, Bill
Joy. Since Sun's inception in the early '80s, it has steadily pushed one idea: "The network is the
computer." At the time though, Sun was competing in a relatively small workstation market, while
Microsoft was beginning its domination of the more mainstream, Intel-based PC world. When Sun
missed the boat on the PC revolution, Joy retreated to Aspen, Colorado, to work on advanced
research. He was committed to accomplishing complex tasks with simple software, and founded the
aptly named Sun Aspen Smallworks.
Of the original members of the small team of programmers assembled in Aspen, James Gosling is
the one who will be remembered as the father of Java. Gosling first made a name for himself in the
early '80s as the author of Gosling Emacs, the first version of the popular Emacs editor that was
written in C and ran under Unix. Gosling Emacs became popular, but was soon eclipsed by a free
version, GNU Emacs, written by Emacs's original designer. By that time, Gosling had moved on to
design Sun's NeWS window system, which briefly contended with the X Window System for
control of the Unix graphical user interface (GUI) desktop in 1987. While some people would argue
that NeWS was superior to X, NeWS lost out because Sun kept it proprietary and didn't publish
source code, while the primary developers of X formed the X Consortium and took the opposite
approach.
Designing NeWS taught Gosling the power of integrating an expressive language with a network-
aware windowing GUI. It also taught Sun that the Internet programming community will refuse to
accept proprietary standards, no matter how good they may be. The seeds of Java's remarkably
permissive licensing scheme were sown by NeWS's failure. Gosling brought what he had learned to
Bill Joy's nascent Aspen project, and in 1992, work on the project led to the founding of the Sun
subsidiary, FirstPerson, Inc. Its mission was to lead Sun into the world of consumer electronics.
The FirstPerson team worked on developing software for information appliances, such as cellular
phones and personal digital assistants (PDAs). The goal was to enable the transfer of information
and real-time applications over cheap infrared and packet-based networks. Memory and bandwidth
limitations dictated small and efficient code. The nature of the applications also demanded they be
safe and robust. Gosling and his teammates began programming in C++, but they soon found
themselves confounded by a language that was too complex, unwieldy, and insecure for the task.
- 5 -
Zgłoś jeśli naruszono regulamin