Java™: The Complete Reference, Seventh Edition
#1

Java™: The Complete Reference, Seventh Edition

.pdf   Java The Complete Reference - 7th Edition [DwzRG].pdf (Size: 6.38 MB / Downloads: 0)
Preface
As I write this, Java is just beginning its second decade. Unlike many other computer
languages whose influence begins to wane over the years, Java’s has grown stronger
with the passage of time. Java leapt to the forefront of Internet programming with
its first release. Each subsequent version has solidified that position. Today, Java is still the
first and best choice for developing web-based applications.
One reason for Java’s success is its agility. Java has rapidly adapted to changes in the
programming environment and to changes in the way that programmers program. Most
importantly, it has not just followed the trends, it has helped create them. Unlike some other
languages that have a revision cycle of approximately 10 years, Java’s release cycle averages
about 1.5 years! Java’s ability to accommodate the fast rate of change in the computing
world is a crucial part of why it has stayed at the forefront of computer language design.
With the release of Java SE 6, Java’s leadership remains unchallenged. If you are programming
for the Internet, you have chosen the right language. Java has been and continues to be the
preeminent language of the Internet.
As many readers will know, this is the seventh edition of the book, which was first
published in 1996. This edition has been updated for Java SE 6. It has also been expanded in
several key areas. Here are two examples: it now includes twice as much coverage of Swing
and a more detailed discussion of resource bundles. Throughout are many other additions
and improvements. In all, dozens of pages of new material have been incorporated.
A Book for All Programmers
This book is for all programmers, whether you are a novice or an experienced pro. The
beginner will find its carefully paced discussions and many examples especially helpful.
Its in-depth coverage of Java’s more advanced features and libraries will appeal to the pro.
For both, it offers a lasting resource and handy reference.
What’s Inside
This book is a comprehensive guide to the Java language, describing its syntax, keywords,
and fundamental programming principles. Significant portions of the Java API library are
also examined. The book is divided into four parts, each focusing on a different aspect of
the Java programming environment.
xxix
Part I presents an in-depth tutorial of the Java language. It begins with the basics,
including such things as data types, control statements, and classes. Part I also discusses
Java’s exception-handling mechanism, multithreading subsystem, packages, and interfaces.
Of course, Java’s newer features, such as generics, annotations, enumerations, and autoboxing
are covered in detail.
Part II examines key aspects of Java’s standard API library. Topics include strings, I/O,
networking, the standard utilities, the Collections Framework, applets, GUI-based controls,
imaging, and concurrency.
Part III looks at three important Java technologies: Java Beans, Swing, and servlets.
Part IV contains two chapters that show examples of Java in action. The first chapter
develops several applets that perform various popular financial calculations, such as
computing the regular payment on a loan or the minimum investment needed to withdraw
a desired monthly annuity. This chapter also shows how to convert those applets into servlets.
The second chapter develops a download manager that oversees the downloading of files. It
includes the ability to start, stop, and resume a transfer. Both chapters are adapted from my
book The Art of Java, which I co-authored with James Holmes.
Don’t Forget: Code on the Web
Remember, the source code for all of the examples in this book is available free-of-charge on
the Web at osborne.com.
Special Thanks
Special thanks to Patrick Naughton. Patrick was one of the creators of the Java language. He also
helped write the first edition of this book. For example, much of the material in Chapters 19, 20,
and 25 was initially provided by Patrick. His insights, expertise, and energy contributed greatly
to the success of this book.
Thanks also go to Joe O’Neil for providing the initial drafts for Chapters 27, 28, 30, and 31.
Joe has helped on several of my books and, as always, his efforts are appreciated.
Finally, many thanks to James Holmes for providing Chapter 32. James is an extraordinary
programmer and author. He was my co-author on The Art of Java and is the author of Struts:
The Complete Reference and a co-author of JSF: The Complete Reference.
HERBERT SCHILDT
November 8, 2006
xxx J a v a : T h e C o m p l e t e R e f e r e n c e
For Further Study
Java: The Complete Reference is your gateway to the Herb Schildt series of programming
books. Here are some others that you will find of interest.
To learn more about Java programming, we recommend the following:
Java: A Beginner’s Guide
Swing: A Beginner’s Guide
The Art Of Java
To learn about C++, you will find these books especially helpful:
C++: The Complete Reference
C++: A Beginner’s Guide
The Art of C++
C++ From the Ground Up
STL Programming From the Ground Up
To learn about C#, we suggest the following Schildt books:
C#: The Complete Reference
C#: A Beginner’s Guide
To learn about the C language, the following titles will be of interest:
C: The Complete Reference
Teach Yourself C
The History and Evolution of Java
To fully understand Java, one must understand the reasons behind its creation, the
forces that shaped it, and the legacy that it inherits. Like the successful computer
languages that came before, Java is a blend of the best elements of its rich heritage
combined with the innovative concepts required by its unique mission. While the remaining
chapters of this book describe the practical aspects of Java—including its syntax, key libraries,
and applications—this chapter explains how and why Java came about, what makes it so
important, and how it has evolved over the years.
Although Java has become inseparably linked with the online environment of the
Internet, it is important to remember that Java is first and foremost a programming language.
Computer language innovation and development occurs for two fundamental reasons:
• To adapt to changing environments and uses
• To implement refinements and improvements in the art of programming
As you will see, the development of Java was driven by both elements in nearly equal
measure.
Java’s Lineage
Java is related to C++, which is a direct descendant of C. Much of the character of Java
is inherited from these two languages. From C, Java derives its syntax. Many of Java’s
object-oriented features were influenced by C++. In fact, several of Java’s defining
characteristics come from—or are responses to—its predecessors. Moreover, the creation of
Java was deeply rooted in the process of refinement and adaptation that has been occurring
in computer programming languages for the past several decades. For these reasons, this
section reviews the sequence of events and forces that led to Java. As you will see, each
innovation in language design was driven by the need to solve a fundamental problem
that the preceding languages could not solve. Java is no exception.
3
The Birth of Modern Programming: C
The C language shook the computer world. Its impact should not be underestimated, because
it fundamentally changed the way programming was approached and thought about. The
creation of C was a direct result of the need for a structured, efficient, high-level language that
could replace assembly code when creating systems programs. As you probably know, when
a computer language is designed, trade-offs are often made, such as the following:
• Ease-of-use versus power
• Safety versus efficiency
• Rigidity versus extensibility
Prior to C, programmers usually had to choose between languages that optimized one set of
traits or the other. For example, although FORTRAN could be used to write fairly efficient
programs for scientific applications, it was not very good for system code. And while BASIC
was easy to learn, it wasn’t very powerful, and its lack of structure made its usefulness
questionable for large programs. Assembly language can be used to produce highly efficient
programs, but it is not easy to learn or use effectively. Further, debugging assembly code
can be quite difficult.
Another compounding problem was that early computer languages such as BASIC,
COBOL, and FORTRAN were not designed around structured principles. Instead, they
relied upon the GOTO as a primary means of program control. As a result, programs
written using these languages tended to produce “spaghetti code”—a mass of tangled
jumps and conditional branches that make a program virtually impossible to understand.
While languages like Pascal are structured, they were not designed for efficiency, and failed
to include certain features necessary to make them applicable to a wide range of programs.
(Specifically, given the standard dialects of Pascal available at the time, it was not practical
to consider using Pascal for systems-level code.)
So, just prior to the invention of C, no one language had reconciled the conflicting
attributes that had dogged earlier efforts. Yet the need for such a language was pressing. By
the early 1970s, the computer revolution was beginning to take hold, and the demand for
software was rapidly outpacing programmers’ ability to produce it. A great deal of effort
was being expended in academic circles in an attempt to create a better computer language.
But, and perhaps most importantly, a secondary force was beginning to be felt. Computer
hardware was finally becoming common enough that a critical mass was being reached.
No longer were computers kept behind locked doors. For the first time, programmers
were gaining virtually unlimited access to their machines. This allowed the freedom to
experiment. It also allowed programmers to begin to create their own tools. On the eve
of C’s creation, the stage was set for a quantum leap forward in computer languages.
Invented and first implemented by Dennis Ritchie on a DEC PDP-11 running the UNIX
operating system, C was the result of a development process that started with an older
language called BCPL, developed by Martin Richards. BCPL influenced a language called
B, invented by Ken Thompson, which led to the development of C in the 1970s. For many
years, the de facto standard for C was the one supplied with the UNIX operating system
and described in The C Programming Language by Brian Kernighan and Dennis Ritchie
(Prentice-Hall, 1978). C was formally standardized in December 1989, when the American
National Standards Institute (ANSI) standard for C was adopted.
4 P a r t I : T h e J a v a L a n g u a g e
C h a p t e r 1 : T h e H i s t o r y a n d E v o l u t i o n o f J a v a 5
The creation of C is considered by many to have marked the beginning of the modern
age of computer languages. It successfully synthesized the conflicting attributes that had
so troubled earlier languages. The result was a powerful, efficient, structured language that
was relatively easy to learn. It also included one other, nearly intangible aspect: it was a
programmer’s language. Prior to the invention of C, computer languages were generally
designed either as academic exercises or by bureaucratic committees. C is different. It was
designed, implemented, and developed by real, working programmers, reflecting the way
that they approached the job of programming. Its features were honed, tested, thought
about, and rethought by the people who actually used the language. The result was a
language that programmers liked to use. Indeed, C quickly attracted many followers
who had a near-religious zeal for it. As such, it found wide and rapid acceptance in the
programmer community. In short, C is a language designed by and for programmers.
As you will see, Java inherited this legacy.
C++: The Next Step
During the late 1970s and early 1980s, C became the dominant computer programming
language, and it is still widely used today. Since C is a successful and useful language, you
might ask why a need for something else existed. The answer is complexity. Throughout the
history of programming, the increasing complexity of programs has driven the need for better
ways to manage that complexity. C++ is a response to that need. To better understand why
managing program complexity is fundamental to the creation of C++, consider the following.
Approaches to programming have changed dramatically since the invention of the
computer. For example, when computers were first invented, programming was done by
manually toggling in the binary machine instructions by use of the front panel. As long as
programs were just a few hundred instructions long, this approach worked. As programs grew,
assembly language was invented so that a programmer could deal with larger, increasingly
complex programs by using symbolic representations of the machine instructions. As programs
continued to grow, high-level languages were introduced that gave the programmer more tools
with which to handle complexity.
The first widespread language was, of course, FORTRAN. While FORTRAN was an
impressive first step, it is hardly a language that encourages clear and easy-to-understand
programs. The 1960s gave birth to structured programming. This is the method of programming
championed by languages such as C. The use of structured languages enabled programmers
to write, for the first time, moderately complex programs fairly easily. However, even with
structured programming methods, once a project reaches a certain size, its complexity exceeds
what a programmer can manage. By the early 1980s, many projects were pushing the structured
approach past its limits. To solve this problem, a new way to program was invented, called
Object-Oriented Programming
object-oriented programming (OOP). Object-oriented programming is discussed in detail later in
this book, but here is a brief definition: OOP is a programming methodology that helps organize
complex programs through the use of inheritance, encapsulation, and polymorphism.
In the final analysis, although C is one of the world’s great programming languages,
there is a limit to its ability to handle complexity. Once the size of a program exceeds a
certain point, it becomes so complex that it is difficult to grasp as a totality. While the
precise size at which this occurs differs, depending upon both the nature of the program
and the programmer, there is always a threshold at which a program becomes
unmanageable. C++ added features that enabled this threshold to be broken, allowing
programmers to comprehend and manage larger programs.
C++ was invented by Bjarne Stroustrup in 1979, while he was working at Bell Laboratories
in Murray Hill, New Jersey. Stroustrup initially called the new language “C with Classes.”
However, in 1983, the name was changed to C++. C++ extends C by adding object-oriented
features. Because C++ is built on the foundation of C, it includes all of C’s features, attributes,
and benefits. This is a crucial reason for the success of C++ as a language. The invention of C++
was not an attempt to create a completely new programming language. Instead, it was an
enhancement to an already highly successful one.
The Stage Is Set for Java
By the end of the 1980s and the early 1990s, object-oriented programming using C++ took
hold. Indeed, for a brief moment it seemed as if programmers had finally found the perfect
language. Because C++ blended the high efficiency and stylistic elements of C with the
object-oriented paradigm, it was a language that could be used to create a wide range of
programs. However, just as in the past, forces were brewing that would, once again, drive
computer language evolution forward. Within a few years, the World Wide Web and the
Internet would reach critical mass. This event would precipitate another revolution in
programming.
The Creation of Java
Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike
Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months to develop the first working
version. This language was initially called “Oak,” but was renamed “Java” in 1995. Between
the initial implementation of Oak in the fall of 1992 and the public announcement of Java in
the spring of 1995, many more people contributed to the design and evolution of the language.
Bill Joy, Arthur van Hoff, Jonathan Payne, Frank Yellin, and Tim Lindholm were key
contributors to the maturing of the original prototype.
Somewhat surprisingly, the original impetus for Java was not the Internet! Instead, the
primary motivation was the need for a platform-independent (that is, architecture-neutral)
language that could be used to create software to be embedded in various consumer electronic
devices, such as microwave ovens and remote controls. As you can probably guess, many
different types of CPUs are used as controllers. The trouble with C and C++ (and most other
languages) is that they are designed to be compiled for a specific target. Although it is possible
to compile a C++ program for just about any type of CPU, to do so requires a full C++ compiler
targeted for that CPU. The problem is that compilers are expensive and time-consuming to
create. An easier—and more cost-efficient—solution was needed. In an attempt to find such a
solution, Gosling and others began work on a portable, platform-independent language that
could be used to produce code that would run on a variety of CPUs under differing
environments. This effort ultimately led to the creation of Java.
About the time that the details of Java were being worked out, a second, and ultimately
more important, factor was emerging that would play a crucial role in the future of Java.
This second force was, of course, the World Wide Web. Had the Web not taken shape at
about the same time that Java was being implemented, Java might have remained a useful
but obscure language for programming consumer electronics. However, with the emergence
of the World Wide Web, Java was propelled to the forefront of computer language design,
because the Web, too, demanded portable programs.
6 P a r t I : T h e J a v a L a n g u a g e
C h a p t e r 1 : T h e H i s t o r y a n d E v o l u t i o n o f J a v a 7
Most programmers learn early in their careers that portable programs are as elusive as they
are desirable. While the quest for a way to create efficient, portable (platform-independent)
programs is nearly as old as the discipline of programming itself, it had taken a back seat to
other, more pressing problems. Further, because (at that time) much of the computer world
had divided itself into the three competing camps of Intel, Macintosh, and UNIX, most
programmers stayed within their fortified boundaries, and the urgent need for portable
code was reduced. However, with the advent of the Internet and the Web, the old problem
of portability returned with a vengeance. After all, the Internet consists of a diverse,
distributed universe populated with various types of computers, operating systems, and
CPUs. Even though many kinds of platforms are attached to the Internet, users would like
them all to be able to run the same program. What was once an irritating but low-priority
problem had become a high-profile necessity.
By 1993, it became obvious to members of the Java design team that the problems of
portability frequently encountered when creating code for embedded controllers are also
found when attempting to create code for the Internet. In fact, the same problem that Java
was initially designed to solve on a small scale could also be applied to the Internet on a
large scale. This realization caused the focus of Java to switch from consumer electronics
to Internet programming. So, while the desire for an architecture-neutral programming
language provided the initial spark, the Internet ultimately led to Java’s large-scale success.
As mentioned earlier, Java derives much of its character from C and C++. This is by
intent. The Java designers knew that using the familiar syntax of C and echoing the
object-oriented features of C++ would make their language appealing to the legions of
experienced C/C++ programmers. In addition to the surface similarities, Java shares some
of the other attributes that helped make C and C++ successful. First, Java was designed,
tested, and refined by real, working programmers. It is a language grounded in the needs
and experiences of the people who devised it. Thus, Java is a programmer’s language.
Second, Java is cohesive and logically consistent. Third, except for those constraints
imposed by the Internet environment, Java gives you, the programmer, full control. If you
program well, your programs reflect it. If you program poorly, your programs reflect that,
too. Put differently, Java is not a language with training wheels. It is a language for
professional programmers.
Because of the similarities between Java and C++, it is tempting to think of Java as simply
the “Internet version of C++.” However, to do so would be a large mistake. Java has significant
practical and philosophical differences. While it is true that Java was influenced by C++, it is
not an enhanced version of C++. For example, Java is neither upwardly nor downwardly
compatible with C++. Of course, the similarities with C++ are significant, and if you are a
C++ programmer, then you will feel right at home with Java. One other point: Java was not
designed to replace C++. Java was designed to solve a certain set of problems. C++ was
designed to solve a different set of problems. Both will coexist for many years to come.
As mentioned at the start of this chapter, computer languages evolve for two reasons:
to adapt to changes in environment and to implement advances in the art of programming.
The environmental change that prompted Java was the need for platform-independent
programs destined for distribution on the Internet. However, Java also embodies changes
in the way that people approach the writing of programs. For example, Java enhanced
and refined the object-oriented paradigm used by C++, added integrated support for
multithreading, and provided a library that simplified Internet access. In the final analysis,
though, it was not the individual features of Java that made it so remarkable. Rather, it was
the language as a whole. Java was the perfect response to the demands of the then newly
emerging, highly distributed computing universe. Java was to Internet programming what
C was to system programming: a revolutionary force that changed the world.
The C# Connection
The reach and power of Java continues to be felt in the world of computer language
development. Many of its innovative features, constructs, and concepts have become part
of the baseline for any new language. The success of Java is simply too important to ignore.
Perhaps the most important example of Java’s influence is C#. Created by Microsoft to
support the .NET Framework, C# is closely related to Java. For example, both share the
same general syntax, support distributed programming, and utilize the same object model.
There are, of course, differences between Java and C#, but the overall “look and feel” of
these languages is very similar. This “cross-pollination” from Java to C# is the strongest
testimonial to date that Java redefined the way we think about and use a computer language.
How Java Changed the Internet
The Internet helped catapult Java to the forefront of programming, and Java, in turn, had a
profound effect on the Internet. In addition to simplifying web programming in general,
Java innovated a new type of networked program called the applet that changed the way
the online world thought about content. Java also addressed some of the thorniest issues
associated with the Internet: portability and security. Let’s look more closely at each of these.
Java Applets
An applet is a special kind of Java program that is designed to be transmitted over the
Internet and automatically executed by a Java-compatible web browser. Furthermore, an
applet is downloaded on demand, without further interaction with the user. If the user
clicks a page link that contains an applet, the applet will be automatically downloaded and run in
the browser. Applets are intended to be small programs. They are typically used to display
data provided by the server, handle user input, or provide simple functions, such as a loan
calculator, that execute locally, rather than on the server. In essence, the applet allows some
functionality to be moved from the server to the client.
The creation of the applet changed Internet programming because it expanded the
universe of objects that can move about freely in cyberspace. In general, there are two very
broad categories of objects that are transmitted between the server and the client: passive
information and dynamic, active programs. For example, when you read your e-mail, you are
viewing passive data. Even when you download a program, the program’s code is still only
passive data until you execute it. By contrast, the applet is a dynamic, self-executing program.
Such a program is an active agent on the client computer, yet it is initiated by the server.
As desirable as dynamic, networked programs are, they also present serious problems
in the areas of security and portability. Obviously, a program that downloads and executes
automatically on the client computer must be prevented from doing harm. It must also be
able to run in a variety of different environments and under different operating systems. As
you will see, Java solved these problems in an effective and elegant way. Let’s look a bit
more closely at each.
8 P a r t I : T h e J a v a L a n g u a g e
Security
As you are likely aware, every time you download a “normal” program, you are taking a
risk, because the code you are downloading might contain a virus, Trojan horse, or other
harmful code. At the core of the problem is the fact that malicious code can cause its damage
because it has gained unauthorized access to system resources. For example, a virus program
might gather private information, such as credit card numbers, bank account balances, and
passwords, by searching the contents of your computer’s local file system. In order for Java
to enable applets to be downloaded and executed on the client computer safely, it was
necessary to prevent an applet from launching such an attack.
Java achieved this protection by confining an applet to the Java execution environment
and not allowing it access to other parts of the computer. (You will see how this is
accomplished shortly.) The ability to download applets with confidence that no harm will
be done and that no security will be breached is considered by many to be the single most
innovative aspect of Java.
Portability
Portability is a major aspect of the Internet because there are many different types of
computers and operating systems connected to it. If a Java program were to be run on
virtually any computer connected to the Internet, there needed to be some way to enable
that program to execute on different systems. For example, in the case of an applet, the
same applet must be able to be downloaded and executed by the wide variety of CPUs,
operating systems, and browsers connected to the Internet. It is not practical to have
different versions of the applet for different computers. The same code must work on all
computers. Therefore, some means of generating portable executable code was needed. As
you will soon see, the same mechanism that helps ensure security also helps create portability.
Java’s Magic: The Bytecode
The key that allows Java to solve both the security and the portability problems just described
is that the output of a Java compiler is not executable code. Rather, it is bytecode. Bytecode is
a highly optimized set of instructions designed to be executed by the Java run-time system,
which is called the Java Virtual Machine (JVM). In essence, the original JVM was designed as
an interpreter for bytecode. This may come as a bit of a surprise since many modern languages
are designed to be compiled into executable code because of performance concerns. However,
the fact that a Java program is executed by the JVM helps solve the major problems associated
with web-based programs. Here is why.
Translating a Java program into bytecode makes it much easier to run a program in
a wide variety of environments because only the JVM needs to be implemented for each
platform. Once the run-time package exists for a given system, any Java program can run
on it. Remember, although the details of the JVM will differ from platform to platform, all
understand the same Java bytecode. If a Java program were compiled to native code, then
different versions of the same program would have to exist for each type of CPU connected
to the Internet. This is, of course, not a feasible solution. Thus, the execution of bytecode by
the JVM is the easiest way to create truly portable programs.
The fact that a Java program is executed by the JVM also helps to make it secure.
Because the JVM is in control, it can contain the program and prevent it from generating
C h a p t e r 1 : T h e H i s t o r y a n d E v o l u t i o n o f J a v a 9
side effects outside of the system. As you will see, safety is also enhanced by certain
restrictions that exist in the Java language.
In general, when a program is compiled to an intermediate form and then interpreted
by a virtual machine, it runs slower than it would run if compiled to executable code.
However, with Java, the differential between the two is not so great. Because bytecode has
been highly optimized, the use of bytecode enables the JVM to execute programs much
faster than you might expect.
Although Java was designed as an interpreted language, there is nothing about Java that
prevents on-the-fly compilation of bytecode into native code in order to boost performance.
For this reason, Sun began supplying its HotSpot technology not long after Java’s initial
release. HotSpot provides a Just-In-Time (JIT) compiler for bytecode. When a JIT compiler
is part of the JVM, selected portions of bytecode are compiled into executable code in real
time, on a piece-by-piece, demand basis. It is important to understand that it is not practical
to compile an entire Java program into executable code all at once, because Java performs
various run-time checks that can be done only at run time. Instead, a JIT compiler compiles
code as it is needed, during execution. Furthermore, not all sequences of bytecode are
compiled—only those that will benefit from compilation. The remaining code is simply
interpreted. However, the just-in-time approach still yields a significant performance boost.
Even when dynamic compilation is applied to bytecode, the portability and safety features
still apply, because the JVM is still in charge of the execution environment.
Servlets: Java on the Server Side
As useful as applets can be, they are just one half of the client/server equation. Not long
after the initial release of Java, it became obvious that Java would also be useful on the
server side. The result was the servlet. A servlet is a small program that executes on the
server. Just as applets dynamically extend the functionality of a web browser, servlets
dynamically extend the functionality of a web server. Thus, with the advent of the servlet,
Java spanned both sides of the client/server connection.
Servlets are used to create dynamically generated content that is then served to the
client. For example, an online store might use a servlet to look up the price for an item in a
database. The price information is then used to dynamically generate a web page that is sent
to the browser. Although dynamically generated content is available through mechanisms such
as CGI (Common Gateway Interface), the servlet offers several advantages, including
increased performance.
Because servlets (like all Java programs) are compiled into bytecode and executed
by the JVM, they are highly portable. Thus, the same servlet can be used in a variety of
different server environments. The only requirements are that the server support the JVM
and a servlet container.
The Java Buzzwords
No discussion of Java’s history is complete without a look at the Java buzzwords. Although
the fundamental forces that necessitated the invention of Java are portability and security,
other factors also played an important role in molding the final form of the language. The
key considerations were summed up by the Java team in the following list of buzzwords:
10 P a r t I : T h e J a v a L a n g u a g e
• Simple
• Secure
• Portable
• Object-oriented
• Robust
• Multithreaded
• Architecture-neutral
• Interpreted
• High performance
• Distributed
• Dynamic
Two of these buzzwords have already been discussed: secure and portable. Let’s
examine what each of the others implies.
Simple
Java was designed to be easy for the professional programmer to learn and use effectively.
Assuming that you have some programming experience, you will not find Java hard to master.
If you already understand the basic concepts of object-oriented programming, learning Java
will be even easier. Best of all, if you are an experienced C++ programmer, moving to Java will
require very little effort. Because Java inherits the C/C++ syntax and many of the
object-oriented features of C++, most programmers have little trouble learning Java.
Object-Oriented
Although influenced by its predecessors, Java was not designed to be source-code compatible
with any other language. This allowed the Java team the freedom to design with a blank
slate. One outcome of this was a clean, usable, pragmatic approach to objects. Borrowing
liberally from many seminal object-software environments of the last few decades, Java
manages to strike a balance between the purist’s “everything is an object” paradigm and
the pragmatist’s “stay out of my way” model. The object model in Java is simple and easy
to extend, while primitive types, such as integers, are kept as high-performance nonobjects.
Robust
The multiplatformed environment of the Web places extraordinary demands on a program,
because the program must execute reliably in a variety of systems. Thus, the ability to create
robust programs was given a high priority in the design of Java. To gain reliability, Java
restricts you in a few key areas to force you to find your mistakes early in program
development. At the same time, Java frees you from having to worry about many of the
most common causes of programming errors. Because Java is a strictly typed language, it
checks your code at compile time. However, it also checks your code at run time. Many
hard-to-track-down bugs that often turn up in hard-to-reproduce run-time situations are
simply impossible to create in Java. Knowing that what you have written will behave in a
predictable way under diverse conditions is a key feature of Java.
C h a p t e r 1 : T h e H i s t o r y a n d E v o l u t i o n o f J a v a 11
12 P a r t I : T h e J a v a L a n g u a g e
To better understand how Java is robust, consider two of the main reasons for program
failure: memory management mistakes and mishandled exceptional conditions (that is,
run-time errors). Memory management can be a difficult, tedious task in traditional
programming environments. For example, in C/C++, the programmer must manually allocate
and free all dynamic memory. This sometimes leads to problems, because programmers will
either forget to free memory that has been previously allocated or, worse, try to free some
memory that another part of their code is still using. Java virtually eliminates these problems
by managing memory allocation and deallocation for you. (In fact, deallocation is completely
automatic, because Java provides garbage collection for unused objects.) Exceptional conditions
in traditional environments often arise in situations such as division by zero or “file not found,”
and they must be managed with clumsy and hard-to-read constructs. Java helps in this area
by providing object-oriented exception handling. In a well-written Java program, all run-time
errors can—and should—be managed by your program.
Multithreaded
Java was designed to meet the real-world requirement of creating interactive, networked
programs. To accomplish this, Java supports multithreaded programming, which allows
you to write programs that do many things simultaneously. The Java run-time system
comes with an elegant yet sophisticated solution for multiprocess synchronization that
enables you to construct smoothly running interactive systems. Java’s easy-to-use approach
to multithreading allows you to think about the specific behavior of your program, not the
multitasking subsystem.
Architecture-Neutral
Acentral issue for the Java designers was that of code longevity and portability. One of the
main problems facing programmers is that no guarantee exists that if you write a program
today, it will run tomorrow—even on the same machine. Operating system upgrades,
processor upgrades, and changes in core system resources can all combine to make a
program malfunction. The Java designers made several hard decisions in the Java language
and the Java Virtual Machine in an attempt to alter this situation. Their goal was “write
once; run anywhere, any time, forever.” To a great extent, this goal was accomplished.
Interpreted and High Performance
As described earlier, Java enables the creation of cross-platform programs by compiling
into an intermediate representation called Java bytecode. This code can be executed on
any system that implements the Java Virtual Machine. Most previous attempts at
cross-platform solutions have done so at the expense of performance. As explained earlier,
the Java bytecode was carefully designed so that it would be easy to translate directly into
native machine code for very high performance by using a just-in-time compiler. Java run-time
systems that provide this feature lose none of the benefits of the platform-independent code.
Distributed
Java is designed for the distributed environment of the Internet because it handles TCP/IP
protocols. In fact, accessing a resource using a URL is not much different from accessing a
file. Java also supports Remote Method Invocation (RMI). This feature enables a program to
invoke methods across a network.
C h a p t e r 1 : T h e H i s t o r y a n d E v o l u t i o n o f J a v a 13
Dynamic
Java programs carry with them substantial amounts of run-time type information that
is used to verify and resolve accesses to objects at run time. This makes it possible to
dynamically page link code in a safe and expedient manner. This is crucial to the robustness of
the Java environment, in which small fragments of bytecode may be dynamically updated
on a running system.
The Evolution of Java
The initial release of Java was nothing short of revolutionary, but it did not mark the end of
Java’s era of rapid innovation. Unlike most other software systems that usually settle into a
pattern of small, incremental improvements, Java continued to evolve at an explosive pace.
Soon after the release of Java 1.0, the designers of Java had already created Java 1.1. The
features added by Java 1.1 were more significant and substantial than the increase in the
minor revision number would have you think. Java 1.1 added many new library elements,
redefined the way events are handled, and reconfigured many features of the 1.0 library. It
also deprecated (rendered obsolete) several features originally defined by Java 1.0. Thus,
Java 1.1 both added to and subtracted from attributes of its original specification.
The next major release of Java was Java 2, where the “2” indicates “second generation.”
The creation of Java 2 was a watershed event, marking the beginning of Java’s “modern
age.” The first release of Java 2 carried the version number 1.2. It may seem odd that the
first release of Java 2 used the 1.2 version number. The reason is that it originally referred
to the internal version number of the Java libraries, but then was generalized to refer to
the entire release. With Java 2, Sun repackaged the Java product as J2SE (Java 2 Platform
Standard Edition), and the version numbers began to be applied to that product.
Java 2 added support for a number of new features, such as Swing and the Collections
Framework, and it enhanced the Java Virtual Machine and various programming tools. Java
2 also contained a few deprecations. The most important affected the Thread class in which
the methods suspend( ), resume( ), and stop( ) were deprecated.
J2SE 1.3 was the first major upgrade to the original Java 2 release. For the most part,
it added to existing functionality and “tightened up” the development environment. In
general, programs written for version 1.2 and those written for version 1.3 are source-code
compatible. Although version 1.3 contained a smaller set of changes than the preceding
three major releases, it was nevertheless important.
The release of J2SE 1.4 further enhanced Java. This release contained several important
upgrades, enhancements, and additions. For example, it added the new keyword assert,
chained exceptions, and a channel-based I/O subsystem. It also made changes to the
Collections Framework and the networking classes. In addition, numerous small changes
were made throughout. Despite the significant number of new features, version 1.4
maintained nearly 100 percent source-code compatibility with prior versions.
The next release of Java was J2SE 5, and it was revolutionary. Unlike most of the previous
Java upgrades, which offered important, but measured improvements, J2SE 5 fundamentally
expanded the scope, power, and range of the language. To grasp the magnitude of the
changes that J2SE 5 made to Java, consider the following list of its major new features:
• Generics
• Annotations
• Autoboxing and auto-unboxing
14 P a r t I : T h e J a v a L a n g u a g e
• Enumerations
• Enhanced, for-each style for loop
• Variable-length arguments (varargs)
• Static import
• Formatted I/O
• Concurrency utilities
This is not a list of minor tweaks or incremental upgrades. Each item in the list represents
a significant addition to the Java language. Some, such as generics, the enhanced for, and
varargs, introduce new syntax elements. Others, such as autoboxing and auto-unboxing,
alter the semantics of the language. Annotations add an entirely new dimension to
programming. In all cases, the impact of these additions went beyond their direct effects.
They changed the very character of Java itself.
The importance of these new features is reflected in the use of the version number
“5.” The next version number for Java would normally have been 1.5. However, the new
features were so significant that a shift from 1.4 to 1.5 just didn’t seem to express the
magnitude of the change. Instead, Sun elected to increase the version number to 5 as a way
of emphasizing that a major event was taking place. Thus, it was named J2SE 5, and the
developer’s kit was called JDK 5. However, in order to maintain consistency, Sun decided
to use 1.5 as its internal version number, which is also referred to as the developer version
number. The “5” in J2SE 5 is called the product version number.
Java SE 6
The newest release of Java is called Java SE 6, and the material in this book has been updated
to reflect this latest version of Java. With the release of Java SE 6, Sun once again decided to
change the name of the Java platform. First, notice that the “2” has been dropped. Thus, the
platform now has the name Java SE, and the official product name is Java Platform, Standard
Edition 6. As with J2SE 5, the 6 in Java SE 6 is the product version number. The internal,
developer version number is 1.6.
Java SE 6 builds on the base of J2SE 5, adding incremental improvements. Java SE 6
adds no major features to the Java language proper, but it does enhance the API libraries,
add several new packages, and offer improvements to the run time. As it relates to this
book, it is the changes to the core API that are the most notable. Many of the packages
have new classes, and many of the classes have new methods. These changes are indicated
throughout the book. In general, the release of Java SE 6 serves to further solidify the
advances made by J2SE 5.
A Culture of Innovation
Since the beginning, Java has been at the center of a culture of innovation. Its original release
redefined programming for the Internet. The Java Virtual Machine (JVM) and bytecode
changed the way we think about security and portability. The applet (and then the servlet)
made the Web come alive. The Java Community Process (JCP) redefined the way that new
ideas are assimilated into the language. The world of Java has never stood still for very
long. Java SE 6 is the latest release in Java’s ongoing, dynamic history.
2
An Overview of Java
As in all other computer languages, the elements of Java do not exist in isolation.
Rather, they work together to form the language as a whole. However, this
interrelatedness can make it difficult to describe one aspect of Java without
involving several others. Often a discussion of one feature implies prior knowledge of
another. For this reason, this chapter presents a quick overview of several key features
of Java. The material described here will give you a foothold that will allow you to write
and understand simple programs. Most of the topics discussed will be examined in greater
detail in the remaining chapters of Part I.
Object-Oriented Programming
Object-oriented programming (OOP) is at the core of Java. In fact, all Java programs are to at
least some extent object-oriented. OOP is so integral to Java that it is best to understand its
basic principles before you begin writing even simple Java programs. Therefore, this chapter
begins with a discussion of the theoretical aspects of OOP.
Two Paradigms
All computer programs consist of two elements: code and data. Furthermore, a program can
be conceptually organized around its code or around its data. That is, some programs are
written around “what is happening” and others are written around “who is being affected.”
These are the two paradigms that govern how a program is constructed. The first way is
called the process-oriented model. This approach characterizes a program as a series of linear
steps (that is, code). The process-oriented model can be thought of as code acting on data.
Procedural languages such as C employ this model to considerable success. However, as
mentioned in Chapter 1, problems with this approach appear as programs grow larger and
more complex.
To manage increasing complexity, the second approach, called object-oriented programming,
was conceived. Object-oriented programming organizes a program around its data (that is,
objects) and a set of well-defined interfaces to that data. An object-oriented program can be
characterized as data controlling access to code. As you will see, by switching the controlling
entity to data, you can achieve several organizational benefits.
15
Abstraction
An essential element of object-oriented programming is abstraction. Humans manage
complexity through abstraction. For example, people do not think of a car as a set of tens
of thousands of individual parts. They think of it as a well-defined object with its own
unique behavior. This abstraction allows people to use a car to drive to the grocery store
without being overwhelmed by the complexity of the parts that form the car. They can
ignore the details of how the engine, transmission, and braking systems work. Instead,
they are free to utilize the object as a whole.
Apowerful way to manage abstraction is through the use of hierarchical classifications.
This allows you to layer the semantics of complex systems, breaking them into more
manageable pieces. From the outside, the car is a single object. Once inside, you see that
the car consists of several subsystems: steering, brakes, sound system, seat belts, heating,
cellular phone, and so on. In turn, each of these subsystems is made up of more specialized
units. For instance, the sound system consists of a radio, a CD player, and/or a tape player.
The point is that you manage the complexity of the car (or any other complex system)
through the use of hierarchical abstractions.
Hierarchical abstractions of complex systems can also be applied to computer programs.
The data from a traditional process-oriented program can be transformed by abstraction
into its component objects. Asequence of process steps can become a collection of messages
between these objects. Thus, each of these objects describes its own unique behavior. You
can treat these objects as concrete entities that respond to messages telling them to do something.
This is the essence of object-oriented programming.
Object-oriented concepts form the heart of Java just as they form the basis for human
understanding. It is important that you understand how these concepts translate into
programs. As you will see, object-oriented programming is a powerful and natural paradigm
for creating programs that survive the inevitable changes accompanying the life cycle of any
major software project, including conception, growth, and aging. For example, once you
have well-defined objects and clean, reliable interfaces to those objects, you can gracefully
decommission or replace parts of an older system without fear.
The Three OOP Principles
All object-oriented programming languages provide mechanisms that help you implement
the object-oriented model. They are encapsulation, inheritance, and polymorphism. Let’s take
a look at these concepts now.
Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and
keeps both safe from outside interference and misuse. One way to think about encapsulation
is as a protective wrapper that prevents the code and data from being arbitrarily accessed
by other code defined outside the wrapper. Access to the code and data inside the wrapper
is tightly controlled through a well-defined interface. To relate this to the real world, consider
the automatic transmission on an automobile. It encapsulates hundreds of bits of information
about your engine, such as how much you are accelerating, the pitch of the surface you are
on, and the position of the shift lever. You, as the user, have only one method of affecting
16 P a r t I : T h e J a v a L a n g u a g e
C h a p t e r 2 : A n O v e r v i e w o f J a v a 17
this complex encapsulation: by moving the gear-shift lever. You can’t affect the transmission
by using the turn signal or windshield wipers, for example. Thus, the gear-shift lever is a
well-defined (indeed, unique) interface to the transmission. Further, what occurs inside the
transmission does not affect objects outside the transmission. For example, shifting gears
does not turn on the headlights! Because an automatic transmission is encapsulated, dozens
of car manufacturers can implement one in any way they please. However, from the driver’s
point of view, they all work the same. This same idea can be applied to programming.
The power of encapsulated code is that everyone knows how to access it and thus can use
it regardless of the implementation details—and without fear of unexpected side effects.
In Java, the basis of encapsulation is the class. Although the class will be examined in great
detail later in this book, the following brief discussion will be helpful now. A class defines
the structure and behavior (data and code) that will be shared by a set of objects. Each object
of a given class contains the structure and behavior defined by the class, as if it were stamped
out by a mold in the shape of the class. For this reason, objects are sometimes referred to as
instances of a class. Thus, a class is a logical construct; an object has physical reality.
When you create a class, you will specify the code and data that constitute that class.
Collectively, these elements are called members of the class. Specifically, the data defined by
the class are referred to as member variables or instance variables. The code that operates on
that data is referred to as member methods or just methods. (If you are familiar with C/C++, it
may help to know that what a Java programmer calls a method, a C/C++ programmer calls a
function.) In properly written Java programs, the methods define how the member variables
can be used. This means that the behavior and interface of a class are defined by the methods
that operate on its instance data.
Since the purpose of a class is to encapsulate complexity, there are mechanisms for
hiding the complexity of the implementation inside the class. Each method or variable in a
class may be marked private or public. The public interface of a class represents everything
that external users of the class need to know, or may know. The private methods and data
can only be accessed by code that is a member of the class. Therefore, any other code that
is not a member of the class cannot access a private method or variable. Since the private
members of a class may only be accessed by other parts of your program through the class’
public methods, you can ensure that no improper actions take place. Of course, this means
that the public interface should be carefully designed not to expose too much of the inner
workings of a class (see Figure 2-1).
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page
Popular Searches: pdf on seventh sense technology, crucial c300, oop aspnet, how many academics, complete project in java free download, fact about um66 called so, dinnamalar 02 10 2014 edition,

[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  Privacy-Preserving Updates to Anonymous and Confidential Databases - JAVA project uploader 3 2,169 23-12-2012, 07:35 PM
Last Post: mr.patil1234
  ELECTRICAL ENGINEERING LIST OF REFERENCE JOURNALS seminar addict 1 1,645 12-10-2012, 12:39 PM
Last Post: seminar details
  An Ontology-Supported Web Focused-Crawler for Java Programs project uploader 0 1,159 08-06-2012, 11:31 AM
Last Post: project uploader
  Building a Java chat server seminar details 0 1,321 07-06-2012, 12:07 PM
Last Post: seminar details
  Understanding the Java ClassLoader seminar paper 0 847 15-03-2012, 02:37 PM
Last Post: seminar paper
  Java Basics project uploader 0 856 14-03-2012, 10:19 AM
Last Post: project uploader
  Object Oriented Programming with Java seminar paper 0 841 12-03-2012, 03:40 PM
Last Post: seminar paper
  Quick Reference to C Programs seminar paper 0 783 06-03-2012, 02:27 PM
Last Post: seminar paper
  SQL Built−ins Pocket Reference seminar paper 0 810 03-03-2012, 02:37 PM
Last Post: seminar paper
  Eclipse and Java seminar paper 0 1,175 24-02-2012, 04:51 PM
Last Post: seminar paper

Forum Jump: