📄 ch1.htm
字号:
was already claimed and they changed the name to Java, they hada powerful, yet simple, language on their hands.<P>More importantly, Java was a platform-neutral language, whichmeant that programs developed with Java could run on any computersystem with no changes. This platform independence was attainedby using a special format for compiled Java programs. This fileformat, called byte-code, could be read and executed by any computersystem that has a Java interpreter. The Java interpreter, of course,must be written specially for the system on which it will run.<P>In 1993, after the World Wide Web had transformed the text-basedInternet into a graphics-rich environment, the Java team realizedthat the language they had developed would be perfect for Webprogramming. The team came up with the concept of Web applets,small programs that could be included in Web pages, and even wentso far as to create a complete Web browser (now called HotJava)that demonstrated the language's power.<P>In the second quarter of 1995, Sun Microsystems officially announcedJava. The "new" language was quickly embraced as a powerfultool for developing Internet applications. Netscape Communications,the developer of the popular Netscape Navigator Web browser (Figure1.2), added support for Java to its new Netscape Navigator 2.0.Other Internet software developers are sure to follow suit, includingMicrosoft, whose Internet Explorer 3 (currently in beta) offersJava support. After more than five years of development, Javahas found its home.<P><A HREF="f1-2.gif"><B> Figure 1.2 : </B><I>The new Netscape Navigator 2.0 Web browser is Java capable.</I></A><P><H2><A NAME="IntroducingJava"><FONT SIZE=5 COLOR=#Ff0000>Introducing Java</FONT></A></H2><P>By now, you may be curious why Java is considered such a powerfultool for Internet development projects. You already know thatJava is a simplified version of C++. Anyone who has struggledwith learning C++ knows that the key word in the previous sentenceis "simplified." C++ added so much to the C languagethat even professional programmers often have difficulty makingthe transition.<P>According to Sun Microsystems, Java is "simple, object-oriented,statically typed, compiled, architecture neutral, multi-threaded,garbage collected, robust, secure, and extensible." That'sa mouthful, but this description of Java probably doesn't helpyou understand the language much. The following list of Java'sattributes, however, should clear out some of the cobwebs:<UL><LI>Simple. Java's developers deliberately left out many of theunnecessary features of other high-level programming languages.For example, Java does not support pointer math, implicit typecasting, structures or unions, operator overloading, templates,header files, or multiple inheritance.<LI>Object-oriented. Just like C++, Java uses classes to organizecode into logical modules. At runtime, a program creates objectsfrom the classes. Java classes can inherit from other classes,but multiple inheritance, wherein a class inherits methods andfields from more than one class, is not allowed.<LI>Statically typed. All objects used in a program must be declaredbefore they are used. This enables the Java compiler to locateand report type conflicts.<LI>Compiled. Before you can run a program written in the Javalanguage, the program must be compiled by the Java compiler. Thecompilation results in a "byte-code" file that, whilesimilar to a machine-code file, can be executed under any operatingsystem that has a Java interpreter. This interpreter reads inthe byte-code file and translates the byte-code commands intomachine-language commands that can be directly executed by themachine that's running the Java program. You could say, then,that Java is both a compiled and interpreted language.<LI>Multi-threaded. Java programs can contain multiple threadsof execution, which enables programs to handle several tasks concurrently.For example, a multi-threaded program can render an image on thescreen in one thread while continuing to accept keyboard inputfrom the user in the main thread. All applications have at leastone thread, which represents the program's main path of execution.<LI>Garbage collected. Java programs do their own garbage collection,which means that programs are not required to delete objects thatthey allocate in memory. This relieves programmers of virtuallyall memory-management problems.<LI>Robust. Because the Java interpreter checks all system accessperformed within a program, Java programs cannot crash the system.Instead, when a serious error is discovered, Java programs createan exception. This exception can be captured and managed by theprogram without any risk of bringing down the system.<LI>Secure. The Java system not only verifies all memory accessbut also ensures that no viruses are hitching a ride with a runningapplet. Because pointers are not supported by the Java language,programs cannot gain access to areas of the system for which theyhave no authorization.<LI>Extensible. Java programs support native methods, which arefunctions written in another language, usually C++. Support fornative methods enables programmers to write functions that mayexecute faster than the equivalent functions written in Java.Native methods are dynamically linked to the Java program; thatis, they are associated with the program at runtime. As the Javalanguage is further refined for speed, native methods will probablybe unnecessary.<LI>Well-understood. The Java language is based upon technologythat's been developed over many years. For this reason, Java canbe quickly and easily understood by anyone with experience withmodern programming languages such as C++.</UL><P>As you can tell from the preceding list of features, a great dealof thought went into creating a language that would be fairlyeasy to use but still provide the most powerful features of amodern language like C++. Thanks to features such as automaticgarbage collection, programmers can spend more time developingtheir programs rather than wasting valuable man-hours huntingfor hard-to-find memory-allocation bugs. However, features suchas Java's object-oriented nature, as well as its ability to handlemultiple threads of execution, ensure that the language is bothup-to-date and powerful.<H2><A NAME="JavaPrograms"><FONT SIZE=5 COLOR=#Ff0000>Java Programs</FONT></A></H2><P>As I mentioned previously. Java can be used to create two typesof programs: applets and stand-alone applications. An Applet issimply a part of a Web page, just as an image or a line of textcan be. Just as a browser takes care of displaying an image referencedin an HTML document, a Java-enabled browser locates and runs anApplet . When your Java-capable Web browser loads the HTML document,the Java applet is also loaded and executed.<P>Using applets, you can do everything from adding animated graphicsto your Web pages to creating complete games and utilities thatcan be executed over the Internet. Some applets that have alreadybeen created with Java include Bar Chart, which embeds a configurablebar chart in an HTML document; Crossword Puzzle, which enablesusers to solve a crossword puzzle on the Web; and LED Sign, whichpresents a scrolling, computerized message to viewers of the Webpage within which the applet is embedded. Figure 1.3 shows a spreadsheetapplet running in Netscape Navigator 2.0.<P><A HREF="f1-3.gif"><B> Figure 1.3 : </B><I>Applets are small programs that are run from within an HTML document.</I></A><P><P>Although most Java programmers are excited by the ability to createapplets, Java can also be used to create stand-alone applications-thatis, applications that don't need to be embedded in an HTML document.The most well-known application is the HotJava Web browser itself,shown in Figure 1.4. This basic browser is completely writtenin the Java language, showing how Java handles not only normalprogramming tasks such as looping and evaluating mathematicalexpressions, but also how it can handle the complexities of telecommunicationsprogramming.<P><A HREF="f1-4.gif"><B> Figure 1.4 : </B><I>The HotJava Web browser is written entirely in the Java programming language.</I></A><P><H2><A NAME="TheJavaDevelopersKit"><FONT SIZE=5 COLOR=#Ff0000>The Java Developer's Kit</FONT></A></H2><P>Java is actually more than a computer language; it's also a programmingenvironment that includes a complete set of programming tools.These tools include a compiler, an interpreter, a debugger, adisassembler, a profiler, and more. To create a Java program,you first use a text editor to create the source-code file. Youwrite the source code, of course, in the Java language. Aftercompleting the source code, which is always saved with a .javafile extension, you compile the program into its byte-code format,the file for which has the .class file extension. It is the .classfile that the interpreter loads and executes. Because the byte-codefiles are fully portable between operating systems, they can beexecuted on any system that has a Java interpreter.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Note that many of Java's tools require long file names, especially the long extensions .java and .class. Both Windows 95 and Windows NT allow these long file names, even under DOS sessions.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>After compiling and running a Java program, you may discover thatthe source code needs modification. The Java debugger can helpyou find your errors, whereas the Java profiler provides handyinformation about your program. If you run into a compiled Javaprogram that you'd like to see in source-code form, the Java disassemblerwill do the translation for you. Java also includes a programthat creates the files you need to take advantage of native methods(functions written in another language, such as C++). There'seven a program that can create HTML documents from Java source-codefiles. Although all the development tools are DOS applications-thatis, they don't run under Windows-they provide a complete environmentfor creating and managing Java projects.<P>If you're a little confused about how the many Java programs worktogether, don't worry about it. You'll get a chance to learn moreabout the Java tools as you work through this book. At this point,just be aware that Java provides everything you need to createyour own applets and stand-alone applications. In the second partof this book, you'll start learning the Java language, and inthe third part, you'll start using Java's tools to create yourown applets.<H2><A NAME="WhereIsJava"><FONT SIZE=5 COLOR=#Ff0000>Where Is Java?</FONT></A></H2><P>All this talk about Java doesn't do you much good until you getyour own copy of the Java Development Kit (JDK). You'll probablyalso want a copy of HotJava and Netscape Navigator 2.0, so thatyou can try out the Web pages you create with your Java applets.Two versions of the JDK are included on this book's CD-ROM. While
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -