📄 ch1.htm
字号:
and computing, television and computers, consumer appliances andtelecommunications, and so forth. Each of these converging factorscomes from areas of technology that, until recently, have nothad much in common. For example, the technology of fiber opticsdoes not have much to do directly with the problems of computing.At the same time that these <I>disparate</I> technologies haveconverged, there have also been pressures from <I>within</I> computingto bring together disciplines historically considered unrelated.This convergence within computing has been brought about by variouspressures that have arisen:<UL><LI><FONT COLOR=#000000>An overwhelming need to deal with the</FONT><I>software crisis </I>has developed. This term has beencoined to describe problems that have plagued the software community.Software development has been marred by high cost, low quality,and ever-increasing demand; these problems have been both causedand exacerbated by increasing software complexity. Because ofthese trends, there is tremendous interest in any technique thatcan increase the productivity of software developers, improvereliability, and make it easier to design software that tacklescomplex problems. Over the last decade the technology that hasbeen viewed as having the best chance of successfully dealingwith these problems is <I>object-oriented programming</I>. Object-orientedfeatures of abstraction, encapsulation, and modularity are particularlywell suited for making complex real-world problems easier to modeland, hence, design. A true object-oriented language's supportfor inheritance and dynamic binding improves software reuse andalso, as a consequence, programmer productivity. The garbage collectionfacility in some object-oriented languages improves <I>reliability</I>by freeing the programmer from the intricate concerns of memorymanagement.<LI><FONT COLOR=#000000>The predominance of a variety of operatingsystems and platforms has created a heavy demand for </FONT><I>portable</I>software. Programs need to be written that will not only run onseveral different platforms, but do so in a way that fits intothe native "look and feel" of the environment. Furthermore,portable software needs to have the <I>high performance</I> thatis typical of the software written for the target platform. Interestin soft-ware that is portable but slow is waning rapidly.<LI><FONT COLOR=#000000>The rapid growth of computer networkshas been matched by a corresponding rise of interest in </FONT><I>distributedcomputing</I>. This discipline is concerned with the problemsof software distributed across multiple computers. Distributedcomputing issues include interprocess communication, concurrentprocessing, data sharing and replication, and security. Althoughthe Internet is the most well-known distributed environment, itis in just the early stages of using the full potential of distributedcomputing. Until recently, for example, there has been no solutionto the problem of dynamically downloading an application bothefficiently and securely. <I>Efficiency</I> is important sinceyou want to download only the code you need when you need it;you don't want to wait thirty minutes to download a 3MB application.Even more important, you need to be concerned with the <I>security</I>of the code. You won't run applications from the Internet if itmeans giving the green light to hackers around the world to attackyour hard disk. You also do not want a poorly written Internetapplication to crash your system-the software has to be <I>reliable</I>.</UL><P>Each of these problems has been seriously addressed from manyfronts, but no approach has simultaneously addressed all theseproblems. For example, there might be a programming language that'sgood for rapid development but isn't portable, or a developmentenvironment might be portable, but could not work in a distributedenvironment without opening you up to serious breaches of security.A single solution to all these problems has been lacking…untilJava!<H3><A NAME="WhyJavaIsaComprehensiveSolution">Why Java Is a ComprehensiveSolution</A></H3><P>The designers of Java created a programming environment to attackeach and every one of these problems. As a comprehensive solutionto these pressing issues, Java needs to be understood as not justa programming language, but as a general-purpose environment.Its unique combination of a programming language, compiler, andruntime environment provides a general architecture well suitedfor addressing many of the concerns that have been plaguing thecomputing community for years. This general development environmentis often referred to with the solitary term <I>Java</I>.<P>In part, Java's evolution into a comprehensive solution stemsfrom its twisted history of being a language for general consumerelectronics to being one for PDAs and set-top boxes for interactivetelevision. Features common to each of these potential platformsinclude severe memory constraints and the need to support multipleoperating systems. These problems forced the language to addressthe problems of distributed computing, efficiency, security, portability,and reliability. For Java to be a successful language, furthermore,it would also have to address the problems associated with thesoftware crisis-it would need to be object-oriented.<P>Being object-oriented carried another bonus for Java. The object-orientedmessage-passing model of invoking class methods makes it a naturalfor network-based application development. Consequently, beingobject-oriented is one of the cornerstones of Java, so it's agood place to begin a tour of its general architectural features.<H3><A NAME="WhyObjectOrientedIsImportant">Why Object-OrientedIs Important</A></H3><P>Why is it important for Java to be an object-oriented language?As stated before, modern programming languages need to addressthe problems of the software crisis. Perhaps the most seriousdifficulty a software engineer encounters is the inherent complexityinvolved in modeling a real-world problem. Through the past decadesof computing, analysis techniques have come in and out of fashionthat aim to make modeling the world a more comprehensible andstable practice. When you were taking your first classes in programming,you might have seen flowcharts used as a modeling tool. The StructuredAnalysis school of thought uses data flow diagrams to model theworld. These approaches have some contributions to make, but theyhave one serious problem: they are heavily oriented toward the<I>procedural</I> side of the activities being modeled. The problemwith a procedural approach is that it does not translate wellto software that is compact, easy to maintain, and reusable. Inits worst implementations, a system based on the procedural modelhas a different function for every different type of procedurethat could occur. You have probably seen such systems-they areso incomprehensible and hard to follow that the only goal of thepoor soul who has to maintain the system is to "just makeit work."<P>Object-oriented analysis, design, and programming is a radicaldeparture from the procedural model. Its focus is not on the proceduresof the modeled world, but on the <I>objects</I> appearing in it.Objects are a natural thing to model because that's what the worldis made of. Planes, dogs, and computers are all objects. Evenabstract concepts, such as a priority queue, can be thought ofas an object. What all objects have in common is that they have<I>state</I> and <I>behavior</I>. For example, the state of aplane can be partially indicated by its speed and location, butits behavior might be represented by its ability to change altitude.<P>Another feature of objects is that they are self-contained; inother words, they are <I>modular</I>. When modeling a car engine,for example, you can focus on the characteristics of each objectin the engine as opposed to the flow of gas and energy throughit. The object model breaks the engine down into a series of componentsthat can be described on their own terms, as opposed to what theyare in a complex global view of things. Instead of writing a singlehuge procedure describing what's going on in the car, you canfocus on how each of the individual elements behaves.<P>Finally, objects have the quality of being hierarchical. A largecomplex object, a house, for example, consists of other objects,such as its physical structure, the electrical system, the plumbing,and so forth. These are in turn made up of yet other objects,such as a chimney, the light switches, and a sink. These can bebroken down into yet smaller components such as wood, wires, andpipes. By modeling a system based on <I>hierarchy</I>, you canuse the model of smaller, more easily understood objects, suchas a wire, to construct more complex objects, such as a lightswitch. Since the switch is now seen as an object that consistsof yet simpler objects, its complexity is easier to understandand manage.<P>In object-oriented systems, objects communicate through a <I>message-passing</I>mechanism. When an object receives a message, it may change itsstate and behavior as part of the response. For example, sendinga message of "play" to a CD player object might resultin the CD playing a music track. This message-passing aspect ofobjects translates very nicely into the needs of network programming,where transactions are carried out by the mechanism of messages.It is particularly well suited to distributed systems becauseobjects communicate by a standard message-passing mechanism, inwhich the actual location of the objects becomes less important.If your object needs to talk to an object, it isn't particularlyimportant if it is on your computer or a remote host. All thatreally counts is that the object gets your message.<H3><A NAME="JavaasanObjectOrientedLanguage">Java as an Object-OrientedLanguage</A></H3><P>Java is unusual for an object-oriented programming language inthat it is "objects all the way down." Unlike C++, whichis a confusing combination of objects and functions, everythingin Java is an object. Strings are objects, numbers are objects,threads are objects, even applets are objects. Because of this,Java has all the helpful features of object-oriented systems justdescribed. Its core constructs of classes, objects, methods, andinstance variables are, by their very nature, managed in a modularfashion. Java's support for inheritance allows you to build newclasses from other classes. Each class you construct becomes atool that can be used to create yet more complex classes.<P>Java's particular object-oriented implementation allows it tohave yet even more desirable features than those already discussed.It has a runtime <I>garbage collector</I> that removes objectsfrom memory that you no longer need. No longer will your programrun out of memory because you forgot to explicitly delete an object.The garbage collector, which your program doesn't even have tobe aware of, does this for you. Furthermore, Java's total orientationtoward objects removes another construct that has been a blightof programmers-the pointer. Java hides almost all aspects of memorymanagement from the programmer. C or C++ programmers who havehad to deal with complex bugs caused by the misuse of pointersor bad pointer arithmetic will no longer have to deal with thisentire class of problems. In Java, you will never deal with pointers,only objects. Because of this combination of garbage collectionand removal of the pointer construct, Java has generally takenthe problem of memory management away from you. Consequently,programs written in Java are more <I>reliable</I>.<P>Java's unique object-oriented implementation gives it anotherset of desirable characteristics-<I>simplicity</I> and <I>familiarity</I>.In many ways, its syntax is similar to C and C++, thus makingit familiar. On the other hand, it strips out all the programmingconstructs of this language that are historical artifacts contributinglittle to writing object-oriented programs. For example, the structureconstruct is removed because everything in Java is an object.Unions are removed because they make you think too much abouthow memory is laid out, a low-level memory consideration thatJava's garbage collection and removal of pointers tries to abolish.Other procedural features of C and C++ that are removed includefunctions, which are replaced by class methods; preprocessor constructs,such as <TT>typedef</TT> and <TT>ifdef</TT>;the hated <TT>goto</TT> statement;and, of course, pointers. Java also replaces the complex and troublesomemultiple inheritance of C++ by a simpler combination of singleinheritance and interfaces. With all these features in mind, Javabecomes a simpler and easier language to use.<P>Java has all the object-oriented features discussed in the previoussection. It uses message passing to let objects communicate, andit supports <I>dynamic binding</I>, allowing a message to be sentto an object even though its specific type may not be known untilruntime. Abstract classes and interfaces enable you to specifya design without having to worry about a specific implementation.With Java's access control constructs, you can define differentlevels of access to your class's methods and variables that areavailable to external classes.<H3><A NAME="JavaasaPortableEnvironment">Java as a Portable Environment</A></H3><P><I>Portability</I> is critical to success in the emerging worldof networked applications and commerce. On the Internet, you cannotmake assumptions about what kind of platform your applet willrun on. Not only do you have to write software that will run onthe client's underlying architecture-such as 80x86, PowerPC, or68000 series-but you must also ensure it will have the correctlook and feel of the native interface. To make things even tougher,this software needs to be fast and compact.<P>Java takes a multipronged approach to this challenge. At the heartof this approach is the fact that the Java compiler generates<I>bytecodes</I> that are <I>interpreted</I> at runtime. The factthat bytecodes are generated is important because it avoids theproblem of basing the binary code on a basic set of primitivetypes-such as integers and floating point-that would be tied toa specific platform. For example, even something as simple asan integer data type is implemented in a different manner on differentplatforms (16-bit on some, 32-bit on others). Java defines itsown set of primitive data types and reconstructs large 16-bitor 32-bit values at runtime by composing them out of individualbytecodes. Java consistently uses the big-endian format to dothis, thus avoiding another portability conflict about how platformsstore primitive data types. You can also quickly and efficientlyconvert the bytecodes at runtime to the underlying native formatsif necessary. In short, Java's underlying encoding scheme is <I>architecture-neutral</I>.<P>The bytecode-based system is important to writing a portable interpreter.The bytecodes generated by the compiler are based on the specificationof a <I>Java Virtual Machine,</I> which, as its name suggests,is not a specific hardware platform but a machine implementedin software. The virtual machine is very similar to a real CPUwith its own instruction set, storage formats, and registers.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -