She diligently read and commented on every page of this book, at least twice. And, to Kenna Moser, who continues to support my aspirations with love, encouragement, and great double lattes. It often seems that the methods and techniques we applied yesterday are out-of-date today. Of course, this rapid rate of change is also one of the things that keeps programming exciting. There is always something new on the horizon.
Perhaps no language better exemplifies the preceding statements than Java. Moreover, during the same short period of time, Java has gone through two major revisions. The first occurred when 1. The change in the minor revision number from 1. For example, Java 1. The second major revision, Java 2, is the subject of this book. Java 2 keeps all of the functionality provided by Java 1.
For example, it adds the collections framework, Swing, a new threading model, and numerous API methods and classes. In fact, so many new features have been added that it is not possible to discuss them all in this book. In order to keep pace with Java, this book, too, has gone through rapid revision cycles.
The original version of this book covered Java 1. The second edition covered 1. This, the third edition, covers Java 2. The time from the first edition to the third is less than two and one half years! But then, this book is about Java—and Java moves fast! A Book for All Programmers To use this book does not require any previous programming experience.
Thus, knowledge of those languages helps, but is not necessary. Even if you have never programmed before, you can learn to program in Java using this book. Part I presents an indepth tutorial of the Java language. It begins with the basics, including such things as data types, control statements, and classes. Part II examines the standard Java library.
Part IV presents a number of high-powered Java applets, which serve as extended examples of the way Java can be applied. The final applet, called Scrabblet, is a complete, multiuser networked game. It shows how to handle some of the toughest issues involved in Web-based programming. These include such things as the collections framework, Swing, and the changes to the way multithreading is handled. However, there are also many smaller changes that are sprinkled throughout the Java API. I think that you will find this to be a particularly interesting addition. A Team Effort I have been writing about programming for many years now.
I seldom work with a coauthor. However, because of the special nature of this book, I teamed up with Patrick Naughton, one of the creators of Java. To understand Java is to understand the reasons that drove 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 environment.
While the remaining chapters of this book describe the practical aspects of Java—including its syntax, libraries, and applications—in this chapter, you will learn how and why Java came about, and what makes it so important. 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.
Much of the character of Java is inherited from these two languages. From C, Java derives its syntax. 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 three decades. For these reasons, this section reviews the sequence of events and forces that led up 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. 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, highlevel language that could replace assembly code when creating systems programs. For example, although FORTRAN could be used to write fairly efficient programs for scientific applications, it was not very good for systems code. 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. Instead, they relied upon the GOTO as a primary means of program control. 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. 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. 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. 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 has inherited this legacy. 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.
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 s 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 s, 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 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.
Once a program exceeds somewhere between 25, and , lines of code, it becomes so complex that it is difficult to grasp as a totality. Instead, it was an enhancement to an already highly successful one. Indeed, for a brief moment it seemed as if programmers had finally found the perfect language. 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. It took 18 months to develop the first working version. Between the initial implementation of Oak in the fall of and the public announcement of Java in the spring of , many more people contributed to the design and evolution of the language. 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 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. 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 platformindependent programs is nearly as old as the discipline of programming itself, it had taken a back seat to other, more pressing problems.
Further, because 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 many types of computers, operating systems, and CPUs.
Even though many types 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 , 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. This is by intent. 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. Second, Java is cohesive and logically consistent. Third, except for those constraints imposed by the Internet environment, Java gives you, the — 12 — programmer, full control. If you program well, your programs reflect it.
Java™ 2: The Complete Reference,. Fifth Edition. Herbert Schildt. McGraw-Hill/ Osborne. New York Chicago San Francisco. Lisbon London Madrid Mexico City. Java 2: The Complete Reference, Fifth Edition [Herbert Schildt] on ciwewukide.cf *FREE* shipping on qualifying offers. This fifth edition of a guide to the Java.
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. Java has significant practical and philosophical differences. Java was designed to solve a certain 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. Thus, Java is not a language that exists in isolation. Rather, it is the current instance of an ongoing process begun many years ago.
This fact alone is enough to ensure Java a place in computer language history. Java is to Internet programming what C was to systems programming: a revolutionary force that will change the world. The reason for this is quite simple: Java expands the universe of objects that can move about freely in cyberspace.
In a network, two very broad categories of objects are transmitted between the server and your personal computer: passive information and dynamic, active programs. For example, when you read your e-mail, you are viewing passive data. However, a second type of object can be transmitted to your computer: a dynamic, self-executing program. Such a program is an active agent on the client computer, yet is initiated by the server. For example, a program might be provided by the server to display properly the data that the server is sending. As desirable as dynamic, networked programs are, they also present serious problems in the areas of security and portability.
Prior to Java, cyberspace was effectively closed to half the entities that now live there. As you will see, Java addresses those concerns and, by doing so, has opened the door to an exciting new form of program: the applet. Java Applets and Applications. Java can be used to create two types of programs: applications and applets. An application is a program that runs on your computer, under the operating system of that computer. When used to create applications, Java is not much different from any other computer language.
An applet is an application designed to be transmitted over the Internet and executed by a Java-compatible Web browser. An applet is actually a tiny Java program, dynamically downloaded across the network, just like an image, sound file, or video clip. The important difference is that an applet is an intelligent program, not just an animation or media file. In other words, an applet is a program that can react to user input and dynamically change—not just run the same animation or sound over and over.
As exciting as applets are, they would be nothing more than wishful thinking if Java were not able to address the two fundamental problems associated with them: security and portability.
Prior to Java, most users did not download executable programs frequently, and those who did scanned them for viruses prior to execution. Even so, most users still worried about the possibility of infecting their systems with a virus. In addition to viruses, another type of malicious program exists that must be guarded against. When you use a Java-compatible Web browser, you can safely download Java applets without fear of viral infection or malicious intent. Java achieves this protection by confining a Java program 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 important aspect of Java. Portability As discussed earlier, many types of computers and operating systems are in use throughout the world—and many are connected to the Internet.
For programs to be dynamically downloaded to all the various types of platforms connected to the Internet, some means of generating portable executable code is needed. As you will soon see, the same mechanism that helps ensure security also helps create portability. 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. That is, in its standard form, the. JVM is an interpreter for bytecode. This may come as a bit of a surprise. In fact, most modern languages are designed to be compiled, not interpreted—mostly because of performance concerns. However, the fact that a Java program is executed by the JVM helps solve the major problems associated with downloading programs over the Internet.
Here is why. Translating a Java program into bytecode helps makes it much easier to run a program in a wide variety of environments. The reason is straightforward: 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 interpret 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 interpretation of bytecode is the easiest way to create truly portable programs. The fact that a Java program is interpreted also helps to make it secure. Because the execution of every Java program is under the control of the JVM, the JVM can contain the program and prevent it from generating side effects outside of the system. As you will see, safety is also enhanced by certain restrictions that exist in the Java language.
When a program is interpreted, it generally runs substantially slower than it would run if compiled to executable code. However, with Java, the differential between the two is not so great. The use of bytecode enables the Java run-time system to execute programs much faster than you might expect. Although Java was designed for interpretation, there is technically nothing about Java that prevents on-the-fly compilation of bytecode into native code.
When the JIT compiler is part of the JVM, it compiles bytecode into executable code in real time, on a piece-by-piece, demand basis. It is important to understand that it is not possible to compile an entire Java program into executable code all at once, because Java performs various run-time checks that can be one only at run time. Instead, the JIT compiles code as it is needed, during execution.
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 run-time system which performs the compilation still is in charge of the execution environment. Whether your Java program is actually interpreted in the traditional way or compiled on-the-fly, its functionality is the same. The Java Buzzwords No discussion of the genesis of Java 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. 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. In Java, there are a small number of clearly defined ways to accomplish a given task. 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. The object model in Java is simple and easy to extend, while simple types, such as integers, are kept as highperformance 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. Chapter String Handling. Chapter Exploring java. Chapter java. Chapter Exploring NIO. Chapter Networking. Chapter The Applet Class.
Chapter Event Handling. Chapter Images. Chapter The Concurrency Utilities. Chapter Regular Expressions and Other Packages. Chapter Introducing Swing. Chapter Exploring Swing. Chapter Introducing Swing Menus. Chapter Java Beans. Chapter Introducing Servlets. View all. About the Author. The Nitty Gritty. Drama Fiction Non-fiction Poetry.
The private methods and data can only be accessed by code that is a member of the class. The problem is that compilers are expensive and time-consuming to create. Includes servlets, Swing, and more. This book explains every topic with graphical representations, images, and exercises also. Chapter Exception Handling.