Java 2 - The Complete Reference (5th Edition).pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

Tags

Java programming programming languages software development

Full Transcript

Java 2: ™ The Complete Reference, Fifth Edition About the Author Herbert Schildt is the world’s leading programming author. He is an authority on the C, C++, Java, and C# languages, and is a master Windows programmer. His programming books have sold...

Java 2: ™ The Complete Reference, Fifth Edition About the Author Herbert Schildt is the world’s leading programming author. He is an authority on the C, C++, Java, and C# languages, and is a master Windows programmer. His programming books have sold more that 3 million copies worldwide and have been translated into all major foreign languages. He is the author of numerous bestsellers, including Java 2: The Complete Reference, Java 2: A Beginner's Guide, Java 2 Programmers Reference, C++: The Complete Reference, C: The Complete Reference, and C#: The Complete Reference. Schildt holds a master's degree in computer science from the University of Illinois. He can be reached at his consulting office at (217) 586-4683. Java 2: ™ The Complete Reference, Fifth Edition Herbert Schildt McGraw-Hill/Osborne New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto Copyright © 2002 by The McGraw-HIll Companies, Inc. All rights reserved. Manufactured in the United States of America. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. 0-07-222858-X The material in this eBook also appears in the print version of this title: 0-07-222420-7 All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occur- rence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. For more information, please contact George Hoare, Special Sales, at [email protected] or (212) 904-4069. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS”. McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WAR- RANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no cir- cumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, conse- quential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatso- ever whether such claim or cause arises in contract, tort or otherwise. DOI: 10.1036/007222858X Want to learn more? , We hope you enjoy this McGraw-Hill eBook! If you d like more information about this book, its author, or related books and websites, please click here. Contents at a Glance Part I The Java Language 1 The Genesis of Java.................................... 3 2 An Overview of Java.................................. 17 3 Data Types, Variables, and Arrays....................... 41 4 Operators............................................ 73 5 Control Statements.................................... 99 6 Introducing Classes................................... 129 7 A Closer Look at Methods and Classes................... 155 8 Inheritance........................................... 189 9 Packages and Interfaces................................ 223 10 Exception Handling................................... 249 11 Multithreaded Programming........................... 273 12 I/O, Applets, and Other Topics......................... 313 v vi Java™ 2: The Complete Reference Part II The Java Library 13 String Handling....................................... 347 14 Exploring java.lang.................................... 379 15 java.util Part 1: The Collections Framework............... 439 16 java.util Part 2: More Utility Classes..................... 505 17 Input/Output: Exploring java.io........................ 537 18 Networking.......................................... 587 19 The Applet Class...................................... 627 20 Event Handling....................................... 653 21 Introducing the AWT: Working with Windows, Graphics, and Text.................................. 687 22 Using AWT Controls, Layout Managers, and Menus....... 735 23 Images............................................... 799 24 New I/O, Regular Expressions, and Other Packages....... 843 Part III Software Development Using Java 25 Java Beans............................................ 885 26 A Tour of Swing...................................... 921 27 Servlets.............................................. 949 28 Migrating from C++ to Java............................ 981 Part IV Applying Java 29 The DynamicBillboard Applet.......................... 1011 30 ImageMenu: An Image-Based Web Menu................ 1047 31 The Lavatron Applet: A Sports Arena Display............ 1057 32 Scrabblet: A Multiplayer Word Game.................... 1069 A Using Java’s Documentation Comments.................. 1133 Index............................................... 1141 Contents Preface.......................................................... xxv Part I The Java Language 1 The Genesis of Java................................... 3 Java’s Lineage................................................... 4 The Birth of Modern Programming: C....................... 4 The Need for C++........................................ 6 The Stage Is Set for Java................................... 7 The Creation of Java.............................................. 7 The C# Connection....................................... 9 Why Java Is Important to the Internet............................... 9 Java Applets and Applications............................. 10 Security................................................. 10 Portability............................................... 11 Java’s Magic: The Bytecode........................................ 11 The Java Buzzwords.............................................. 12 Simple.................................................. 13 Object-Oriented.......................................... 13 vii viii Java™ 2: The Complete Reference Robust.................................................. 13 Multithreaded........................................... 14 Architecture-Neutral..................................... 14 Interpreted and High Performance......................... 14 Distributed.............................................. 15 Dynamic................................................ 15 The Continuing Revolution........................................ 15 2 An Overview of Java.................................. 17 Object-Oriented Programming..................................... 18 Two Paradigms.......................................... 18 Abstraction.............................................. 18 The Three OOP Principles................................. 19 A First Simple Program........................................... 25 Entering the Program..................................... 25 Compiling the Program................................... 26 A Closer Look at the First Sample Program.................. 27 A Second Short Program.......................................... 29 Two Control Statements.......................................... 31 The if Statement.......................................... 31 The for Loop............................................. 33 Using Blocks of Code............................................. 35 Lexical Issues.................................................... 37 Whitespace.............................................. 37 Identifiers............................................... 37 Literals................................................. 37 Comments.............................................. 38 Separators............................................... 38 The Java Keywords....................................... 38 The Java Class Libraries........................................... 39 3 Data Types, Variables, and Arrays...................... 41 Java Is a Strongly Typed Language................................. 42 The Simple Types................................................ 42 Integers......................................................... 43 byte.................................................... 44 short.................................................... 44 int...................................................... 44 long.................................................... 45 Floating-Point Types............................................. 45 float.................................................... 46 double.................................................. 46 Characters...................................................... 47 Booleans........................................................ 48 A Closer Look at Literals.......................................... 50 Integer Literals........................................... 50 Floating-Point Literals.................................... 50 Boolean Literals.......................................... 51 Contents ix Character Literals........................................ 51 String Literals............................................ 52 Variables........................................................ 52 Declaring a Variable...................................... 52 Dynamic Initialization.................................... 53 The Scope and Lifetime of Variables........................ 54 Type Conversion and Casting...................................... 57 Java’s Automatic Conversions............................. 57 Casting Incompatible Types............................... 57 Automatic Type Promotion in Expressions.......................... 59 The Type Promotion Rules................................ 60 Arrays.......................................................... 61 One-Dimensional Arrays.................................. 61 Multidimensional Arrays.................................. 64 Alternative Array Declaration Syntax....................... 70 A Few Words About Strings....................................... 70 A Note to C/C++ Programmers About Pointers...................... 71 4 Operators........................................... 73 Arithmetic Operators............................................. 74 The Basic Arithmetic Operators............................ 74 The Modulus Operator.................................... 76 Arithmetic Assignment Operators.......................... 76 Increment and Decrement................................. 78 The Bitwise Operators............................................ 80 The Bitwise Logical Operators............................. 82 The Left Shift............................................ 84 The Right Shift........................................... 86 The Unsigned Right Shift.................................. 87 Bitwise Operator Assignments............................. 89 Relational Operators.............................................. 90 Boolean Logical Operators........................................ 92 Short-Circuit Logical Operators............................ 93 The Assignment Operator......................................... 94 The ? Operator................................................... 95 Operator Precedence............................................. 96 Using Parentheses................................................ 96 5 Control Statements................................... 99 Java’s Selection Statements........................................ 100 if....................................................... 100 switch.................................................. 104 Iteration Statements.............................................. 109 while................................................... 109 do-while................................................ 111 for...................................................... 114 Some for Loop Variations................................. 117 Nested Loops............................................ 119 x Java™ 2: The Complete Reference Jump Statements................................................. 119 Using break............................................. 120 Using continue........................................... 124 return.................................................. 126 6 Introducing Classes................................... 129 Class Fundamentals.............................................. 130 The General Form of a Class............................... 130 A Simple Class........................................... 131 Declaring Objects................................................ 134 A Closer Look at new..................................... 136 Assigning Object Reference Variables............................... 137 Introducing Methods............................................. 138 Adding a Method to the Box Class.......................... 138 Returning a Value........................................ 140 Adding a Method That Takes Parameters.................... 142 Constructors..................................................... 145 Parameterized Constructors............................... 147 The this Keyword................................................ 149 Instance Variable Hiding.................................. 149 Garbage Collection............................................... 150 The finalize( ) Method............................................ 150 A Stack Class.................................................... 151 7 A Closer Look at Methods and Classes.................. 155 Overloading Methods............................................ 156 Overloading Constructors................................. 159 Using Objects as Parameters....................................... 162 A Closer Look at Argument Passing................................ 165 Returning Objects................................................ 168 Recursion....................................................... 169 Introducing Access Control........................................ 172 Understanding static............................................. 176 Introducing final................................................. 178 Arrays Revisited................................................. 179 Introducing Nested and Inner Classes.............................. 181 Exploring the String Class......................................... 185 Using Command-Line Arguments.................................. 188 8 Inheritance.......................................... 189 Inheritance Basics................................................ 190 Member Access and Inheritance............................ 192 A More Practical Example................................. 193 A Superclass Variable Can Reference a Subclass Object........ 196 Using super..................................................... 197 Using super to Call Superclass Constructors................. 197 A Second Use for super................................... 202 Contents xi Creating a Multilevel Hierarchy.................................... 203 When Constructors Are Called..................................... 207 Method Overriding............................................... 208 Dynamic Method Dispatch........................................ 211 Why Overridden Methods?................................ 213 Applying Method Overriding.............................. 214 Using Abstract Classes............................................ 216 Using final with Inheritance....................................... 219 Using final to Prevent Overriding.......................... 219 Using final to Prevent Inheritance.......................... 220 The Object Class................................................. 220 9 Packages and Interfaces............................... 223 Packages........................................................ 224 Defining a Package....................................... 225 Finding Packages and CLASSPATH........................ 226 A Short Package Example................................. 226 Access Protection................................................ 227 An Access Example....................................... 229 Importing Packages.............................................. 232 Interfaces....................................................... 235 Defining an Interface..................................... 235 Implementing Interfaces.................................. 236 Applying Interfaces...................................... 239 Variables in Interfaces.................................... 243 Interfaces Can Be Extended................................ 246 10 Exception Handling.................................. 249 Exception-Handling Fundamentals................................. 250 Exception Types................................................. 251 Uncaught Exceptions............................................. 251 Using try and catch............................................... 253 Displaying a Description of an Exception.................... 254 Multiple catch Clauses............................................ 255 Nested try Statements............................................ 257 throw........................................................... 260 throws.......................................................... 261 finally.......................................................... 263 Java’s Built-in Exceptions......................................... 265 Creating Your Own Exception Subclasses........................... 267 Chained Exceptions.............................................. 269 Using Exceptions................................................. 271 11 Multithreaded Programming.......................... 273 The Java Thread Model........................................... 275 Thread Priorities......................................... 275 Synchronization.......................................... 276 xii Java™ 2: The Complete Reference Messaging............................................... 276 The Thread Class and the Runnable Interface................ 277 The Main Thread................................................. 277 Creating a Thread................................................ 280 Implementing Runnable.................................. 280 Extending Thread........................................ 282 Choosing an Approach.................................... 284 Creating Multiple Threads........................................ 284 Using isAlive( ) and join( )......................................... 286 Thread Priorities................................................. 289 Synchronization................................................. 292 Using Synchronized Methods.............................. 292 The synchronized Statement............................... 295 Interthread Communication....................................... 297 Deadlock................................................ 302 Suspending, Resuming, and Stopping Threads....................... 305 Suspending, Resuming, and Stopping Threads Using Java 1.1 and Earlier.................................... 305 Suspending, Resuming, and Stopping Threads Using Java 2................................................ 308 Using Multithreading............................................. 311 12 I/O, Applets, and Other Topics........................ 313 I/O Basics....................................................... 314 Streams................................................. 314 Byte Streams and Character Streams........................ 315 The Predefined Streams................................... 318 Reading Console Input............................................ 318 Reading Characters....................................... 319 Reading Strings.......................................... 320 Writing Console Output.......................................... 322 The PrintWriter Class............................................. 323 Reading and Writing Files......................................... 324 Applet Fundamentals............................................. 328 The transient and volatile Modifiers................................ 331 Using instanceof................................................. 332 strictfp.......................................................... 335 Native Methods.................................................. 335 Problems with Native Methods............................ 340 Using assert..................................................... 340 Assertion Enabling and Disabling Options................... 343 Part II The Java Library 13 String Handling...................................... 347 The String Constructors........................................... 348 String Length.................................................... 351 Contents xiii Special String Operations......................................... 351 String Literals............................................ 351 String Concatenation..................................... 352 String Concatenation with Other Data Types................. 352 String Conversion and toString( )........................... 353 Character Extraction.............................................. 355 charAt( )................................................ 355 getChars( ).............................................. 355 getBytes( )............................................... 356 toCharArray( )........................................... 356 String Comparison............................................... 356 equals( ) and equalsIgnoreCase( )........................... 357 regionMatches( )......................................... 358 startsWith( ) and endsWith( ).............................. 358 equals( ) Versus ==....................................... 359 compareTo( )............................................ 359 Searching Strings................................................. 361 Modifying a String............................................... 363 substring( ).............................................. 363 concat( )................................................. 364 replace( )................................................ 364 trim( ).................................................. 365 Data Conversion Using valueOf( ).................................. 366 Changing the Case of Characters Within a String..................... 367 String Methods Added by Java 2, Version 1.4........................ 368 StringBuffer..................................................... 369 StringBuffer Constructors................................. 369 length( ) and capacity( )................................... 369 ensureCapacity( )........................................ 370 setLength( )............................................. 370 charAt( ) and setCharAt( )................................. 371 getChars( ).............................................. 371 append( )............................................... 372 insert( )................................................. 373 reverse( )................................................ 373 delete( ) and deleteCharAt( )............................... 374 replace( )................................................ 375 substring( ).............................................. 375 StringBuffer Methods Added by Java 2, Version 1.4........... 376 14 Exploring java.lang................................... 379 Simple Type Wrappers............................................ 380 Number................................................. 381 Double and Float......................................... 381 Byte, Short, Integer, and Long.............................. 387 Character............................................... 397 Boolean................................................. 401 Void............................................................ 402 Process......................................................... 402 xiv Java™ 2: The Complete Reference Runtime........................................................ 403 Memory Management.................................... 405 Executing Other Programs................................ 406 System.......................................................... 407 Using currentTimeMillis( ) to Time Program Execution........ 410 Using arraycopy( )....................................... 411 Environment Properties................................... 412 Object.......................................................... 412 Using clone( ) and the Cloneable Interface........................... 412 Class........................................................... 416 ClassLoader..................................................... 419 Math........................................................... 420 Transcendental Functions................................. 420 Exponential Functions.................................... 420 Rounding Functions...................................... 421 Miscellaneous Math Methods.............................. 422 StrictMath....................................................... 422 Compiler........................................................ 423 Thread, ThreadGroup, and Runnable............................... 423 The Runnable Interface................................... 423 Thread.................................................. 423 ThreadGroup............................................ 426 ThreadLocal and InheritableThreadLocal............................ 432 Package......................................................... 432 RuntimePermission.............................................. 434 Throwable...................................................... 434 SecurityManager................................................. 434 StackTraceElement............................................... 435 The CharSequence Interface....................................... 436 The Comparable Interface......................................... 436 The java.lang.ref and java.lang.reflect Packages...................... 437 java.lang.ref............................................. 437 java.lang.reflect.......................................... 437 15 java.util Part 1: The Collections Framework.............. 439 Collections Overview............................................. 441 The Collection Interfaces.......................................... 442 The Collection Interface................................... 443 The List Interface......................................... 445 The Set Interface......................................... 447 The SortedSet Interface.................................... 447 The Collection Classes............................................ 448 The ArrayList Class...................................... 449 The LinkedList Class..................................... 452 The HashSet Class........................................ 454 The LinkedHashSet Class................................. 456 The TreeSet Class........................................ 456 Accessing a Collection via an Iterator............................... 457 Using an Iterator......................................... 457 Contents xv Storing User-Defined Classes in Collections......................... 460 The RandomAccess Interface...................................... 462 Working with Maps.............................................. 462 The Map Interfaces....................................... 462 The Map Classes......................................... 466 Comparators.................................................... 471 Using a Comparator...................................... 472 The Collection Algorithms........................................ 475 Arrays.......................................................... 480 The Legacy Classes and Interfaces.................................. 484 The Enumeration Interface................................ 484 Vector.................................................. 485 Stack................................................... 490 Dictionary............................................... 492 Hashtable............................................... 494 Properties............................................... 498 Using store( ) and load( ).................................. 502 Collections Summary............................................. 504 16 java.util Part 2: More Utility Classes.................... 505 StringTokenizer.................................................. 506 BitSet........................................................... 508 Date............................................................ 512 Date Comparison........................................ 514 Calendar........................................................ 514 GregorianCalendar............................................... 519 TimeZone....................................................... 521 SimpleTimeZone................................................. 522 Locale.......................................................... 523 Random........................................................ 524 Observable...................................................... 527 The Observer Interface.................................... 528 An Observer Example.................................... 528 Timer and TimerTask............................................. 531 Currency........................................................ 534 The java.util.zip Package.......................................... 536 The java.util.jar Package.......................................... 536 17 Input/Output: Exploring java.io....................... 537 The Java I/O Classes and Interfaces................................ 538 File............................................................. 539 Directories.............................................. 542 Using FilenameFilter..................................... 543 The listFiles( ) Alternative................................. 544 Creating Directories...................................... 545 The Stream Classes............................................... 545 The Byte Streams................................................. 546 xvi Java™ 2: The Complete Reference InputStream............................................. 546 OutputStream........................................... 547 FileInputStream.......................................... 548 FileOutputStream........................................ 550 ByteArrayInputStream.................................... 552 ByteArrayOutputStream.................................. 553 Filtered Byte Streams..................................... 555 Buffered Byte Streams.................................... 555 SequenceInputStream..................................... 559 PrintStream............................................. 561 RandomAccessFile....................................... 561 The Character Streams............................................ 562 Reader.................................................. 562 Writer.................................................. 562 FileReader............................................... 562 FileWriter............................................... 565 CharArrayReader........................................ 566 CharArrayWriter......................................... 567 BufferedReader.......................................... 569 BufferedWriter........................................... 570 PushbackReader......................................... 571 PrintWriter.............................................. 572 Using Stream I/O................................................ 572 Improving wc( ) Using a StreamTokenizer................... 574 Serialization..................................................... 577 Serializable.............................................. 577 Externalizable........................................... 578 ObjectOutput............................................ 578 ObjectOutputStream...................................... 579 ObjectInput.............................................. 580 ObjectInputStream....................................... 581 A Serialization Example................................... 583 Stream Benefits.................................................. 585 18 Networking......................................... 587 Networking Basics............................................... 588 Socket Overview......................................... 588 Client/Server............................................ 589 Reserved Sockets......................................... 589 Proxy Servers............................................ 590 Internet Addressing...................................... 590 Java and the Net................................................. 591 The Networking Classes and Interfaces..................... 592 InetAddress..................................................... 592 Factory Methods......................................... 593 Instance Methods........................................ 594 TCP/IP Client Sockets............................................ 594 Whois.................................................. 596 Contents xvii URL............................................................ 597 Format.................................................. 597 URLConnection.................................................. 599 TCP/IP Server Sockets............................................ 601 A Caching Proxy HTTP Server..................................... 602 Source Code............................................. 602 Datagrams...................................................... 623 DatagramPacket......................................... 624 Datagram Server and Client............................... 624 Inet4Address and Inet6Address.................................... 626 The URI Class................................................... 626 19 The Applet Class..................................... 627 Applet Basics.................................................... 628 The Applet Class......................................... 629 Applet Architecture.............................................. 632 An Applet Skeleton............................................... 632 Applet Initialization and Termination....................... 634 Overriding update( )...................................... 635 Simple Applet Display Methods................................... 636 Requesting Repainting............................................ 638 A Simple Banner Applet.................................. 639 Using the Status Window......................................... 642 The HTML APPLET Tag.......................................... 643 Passing Parameters to Applets..................................... 644 Improving the Banner Applet.............................. 647 getDocumentBase( ) and getCodeBase( )............................ 648 AppletContext and showDocument( )............................... 649 The AudioClip Interface.......................................... 651 The AppletStub Interface.......................................... 652 Outputting to the Console......................................... 652 20 Event Handling...................................... 653 Two Event Handling Mechanisms.................................. 654 The Delegation Event Model....................................... 654 Events.................................................. 655 Event Sources............................................ 655 Event Listeners.......................................... 656 Event Classes.................................................... 656 The ActionEvent Class.................................... 658 The AdjustmentEvent Class............................... 659 The ComponentEvent Class............................... 660 The ContainerEvent Class................................. 660 The FocusEvent Class..................................... 661 The InputEvent Class..................................... 661 The ItemEvent Class...................................... 662 The KeyEvent Class...................................... 663 The MouseEvent Class.................................... 664 xviii Java™ 2: The Complete Reference The MouseWheelEvent Class.............................. 665 The TextEvent Class...................................... 666 The WindowEvent Class.................................. 667 Sources of Events................................................ 668 Event Listener Interfaces.......................................... 669 The ActionListener Interface............................... 670 The AdjustmentListener Interface.......................... 670 The ComponentListener Interface.......................... 670 The ContainerListener Interface............................ 670 The FocusListener Interface................................ 670 The ItemListener Interface................................. 671 The KeyListener Interface................................. 671 The MouseListener Interface............................... 671 The MouseMotionListener Interface........................ 671 The MouseWheelListener Interface......................... 672 The TextListener Interface................................. 672 The WindowFocusListener Interface........................ 672 The WindowListener Interface............................. 672 Using the Delegation Event Model................................. 673 Handling Mouse Events................................... 673 Handling Keyboard Events................................ 676 Adapter Classes.................................................. 680 Inner Classes.................................................... 682 Anonymous Inner Classes................................. 684 21 Introducing the AWT: Working with Windows, Graphics, and Text................................. 687 AWT Classes.................................................... 688 Window Fundamentals........................................... 691 Component.............................................. 691 Container............................................... 692 Panel................................................... 692 Window................................................ 693 Frame.................................................. 693 Canvas................................................. 693 Working with Frame Windows.................................... 693 Setting the Window’s Dimensions.......................... 694 Hiding and Showing a Window............................ 694 Setting a Window’s Title.................................. 694 Closing a Frame Window................................. 694 Creating a Frame Window in an Applet............................. 695 Handling Events in a Frame Window....................... 697 Creating a Windowed Program.................................... 702 Displaying Information Within a Window........................... 704 Working with Graphics........................................... 705 Drawing Lines........................................... 705 Drawing Rectangles...................................... 706 Drawing Ellipses and Circles.............................. 708 Contents xix Drawing Arcs............................................ 709 Drawing Polygons....................................... 710 Sizing Graphics.......................................... 711 Working with Color.............................................. 712 Color Methods........................................... 713 Setting the Current Graphics Color......................... 714 A Color Demonstration Applet............................. 714 Setting the Paint Mode............................................ 715 Working with Fonts.............................................. 717 Determining the Available Fonts........................... 719 Creating and Selecting a Font.............................. 720 Obtaining Font Information............................... 722 Managing Text Output Using FontMetrics........................... 723 Displaying Multiple Lines of Text.......................... 725 Centering Text........................................... 727 Multiline Text Alignment................................. 728 Exploring Text and Graphics...................................... 733 22 Using AWT Controls, Layout Managers, and Menus...... 735 Control Fundamentals............................................ 736 Adding and Removing Controls............................ 736 Responding to Controls................................... 737 Labels.......................................................... 737 Using Buttons................................................... 739 Handling Buttons........................................ 739 Applying Check Boxes............................................ 743 Handling Check Boxes.................................... 743 CheckboxGroup................................................. 745 Choice Controls.................................................. 748 Handling Choice Lists.................................... 748 Using Lists...................................................... 751 Handling Lists.......................................... 752 Managing Scroll Bars............................................. 754 Handling Scroll Bars...................................... 756 Using a TextField................................................ 758 Handling a TextField..................................... 759 Using a TextArea................................................. 761 Understanding Layout Managers.................................. 763 FlowLayout............................................. 764 BorderLayout............................................ 766 Using Insets............................................. 768 GridLayout.............................................. 770 CardLayout............................................. 772 Menu Bars and Menus............................................ 775 Dialog Boxes.................................................... 782 FileDialog....................................................... 788 Handling Events by Extending AWT Components.................... 790 Extending Button........................................ 792 Extending Checkbox...................................... 793 xx Java™ 2: The Complete Reference Extending a Check Box Group............................. 794 Extending Choice........................................ 795 Extending List........................................... 795 Extending Scrollbar....................................... 797 Exploring the Controls, Menus, and Layout Managers................ 798 23 Images.............................................. 799 File Formats..................................................... 800 Image Fundamentals: Creating, Loading, and Displaying.............. 801 Creating an Image Object.................................. 801 Loading an Image........................................ 801 Displaying an Image...................................... 802 ImageObserver.................................................. 803 ImageObserver Example.................................. 805 Double Buffering................................................. 807 MediaTracker.................................................... 811 ImageProducer.................................................. 815 MemoryImageSource..................................... 815 ImageConsumer................................................. 817 PixelGrabber............................................ 818 ImageFilter...................................................... 821 CropImageFilter......................................... 821 RGBImageFilter.......................................... 823 Cell Animation.................................................. 837 Additional Imaging Classes....................................... 840 24 New I/O, Regular Expressions, and Other Packages...... 843 The Core Java API Packages....................................... 844 The New I/O Packages........................................... 847 NIO Fundamentals....................................... 847 Charsets and Selectors.................................... 851 Using the New I/O System................................ 851 Is NIO the Future of I/O Handling?........................ 859 Regular Expression Processing..................................... 859 Pattern.................................................. 859 Matcher................................................. 860 Regular Expression Syntax................................ 861 Demonstrating Pattern Matching........................... 861 Two Pattern-Matching Options............................ 868 Exploring Regular Expressions............................. 869 Reflection....................................................... 869 Remote Method Invocation (RMI).................................. 874 A Simple Client/Server Application Using RMI.............. 874 Text Formatting.................................................. 878 DateFormat Class........................................ 878 SimpleDateFormat Class.................................. 880 Contents xxi Part III Software Development Using Java 25 Java Beans........................................... 885 What Is a Java Bean?.............................................. 886 Advantages of Java Beans......................................... 887 Application Builder Tools......................................... 887 Using the Bean Developer Kit (BDK)................................ 888 Installing the BDK........................................ 888 Starting the BDK......................................... 889 Using the BDK........................................... 889 JAR Files........................................................ 891 Manifest Files............................................ 892 The JAR Utility.......................................... 892 Introspection.................................................... 894 Design Patterns for Properties............................. 894 Design Patterns for Events................................. 896 Methods................................................ 897 Developing a Simple Bean Using the BDK........................... 897 Create a New Bean....................................... 898 Using Bound Properties........................................... 902 Steps................................................... 902 Using the BeanInfo Interface....................................... 903 Constrained Properties........................................... 905 Persistence...................................................... 905 Customizers..................................................... 906 The Java Beans API............................................... 906 Using Bean Builder............................................... 911 Building a Simple Bean Builder Application................. 913 26 A Tour of Swing..................................... 921 JApplet......................................................... 923 Icons and Labels................................................. 923 Text Fields...................................................... 925 Buttons......................................................... 927 The JButton Class........................................ 927 Check Boxes............................................. 930 Radio Buttons........................................... 932 Combo Boxes.................................................... 934 Tabbed Panes.................................................... 936 Scroll Panes..................................................... 939 Trees........................................................... 941 Tables.......................................................... 946 Exploring Swing................................................. 948 27 Servlets............................................. 949 Background..................................................... 950 xxii Java™ 2: The Complete Reference The Life Cycle of a Servlet......................................... 951 Using Tomcat For Servlet Development............................. 951 A Simple Servlet................................................. 953 Create and Compile the Servlet Source Code................. 953 Start Tomcat............................................. 954 Start a Web Browser and Request the Servlet................. 954 The Servlet API.................................................. 954 The javax.servlet Package......................................... 955 The Servlet Interface...................................... 955 The ServletConfig Interface................................ 956 The ServletContext Interface............................... 957 The ServletRequest Interface............................... 957 The ServletResponse Interface............................. 957 The SingleThreadModel Interface.......................... 957 The GenericServlet Class.................................. 960 The ServletInputStream Class.............................. 960 The ServletOutputStream Class............................ 960 The Servlet Exception Classes.............................. 960 Reading Servlet Parameters....................................... 960 The javax.servlet.http Package..................................... 962 The HttpServletRequest Interface........................... 963 The HttpServletResponse Interface......................... 965 The HttpSession Interface................................. 966 The HttpSessionBindingListener Interface................... 967 The Cookie Class......................................... 967 The HttpServlet Class..................................... 969 The HttpSessionEvent Class............................... 970 The HttpSessionBindingEvent Class........................ 971 Handling HTTP Requests and Responses............................ 971 Handling HTTP GET Requests............................. 971 Handling HTTP POST Requests............................ 973 Using Cookies................................................... 975 Session Tracking................................................. 977 Security Issues................................................... 979 28 Migrating from C++ to Java............................ 981 The Differences Between C++ and Java............................. 982 What Java Has Removed from C++......................... 982 New Features Added by Java.............................. 984 Features That Differ...................................... 985 Eliminating Pointers.............................................. 985 Converting Pointer Parameters............................. 986 Converting Pointers that Operate on Arrays................. 988 C++ Reference Parameters Versus Java Reference Parameters.......... 991 Converting C++ Abstract Classes into Java Interfaces................. 995 Converting Default Arguments.................................... 999 Converting C++ Multiple-Inheritance Hierarchies.................... 1001 Destructors Versus Finalization.................................... 1003 Contents xxiii Part IV Applying Java 29 The DynamicBillboard Applet......................... 1011 The APPLET Tag................................................. 1012 Source Code Overview........................................... 1014 DynamicBillboard.java.................................... 1014 BillData.java............................................. 1022 BillTransition.java........................................ 1024 ColumnTransition.java.................................... 1026 FadeTransition.java....................................... 1029 SmashTransition.java..................................... 1033 TearTransition.java....................................... 1036 UnrollTransition.java..................................... 1040 Dynamic Code................................................... 1044 30 ImageMenu: An Image-Based Web Menu............... 1047 The Source Image................................................ 1049 The APPLET Tag................................................. 1050 The Methods.................................................... 1051 init( )................................................... 1051 update( )................................................ 1051 lateInit( )................................................ 1051 paint( ).................................................. 1051 mouseExited( )........................................... 1052 mouseDragged( )......................................... 1052 mouseMoved( ).......................................... 1052 mouseReleased( )........................................ 1053 The Code................................................ 1053 Summary....................................................... 1056 31 The Lavatron Applet: A Sports Arena Display............ 1057 How Lavatron Works............................................. 1059 The Source Code................................................. 1060 The APPLET Tag......................................... 1060 Lavatron.java............................................ 1060 IntHash( )............................................... 1065 Hot Lava........................................................ 1067 32 Scrabblet: A Multiplayer Word Game................... 1069 Network Security Concerns....................................... 1070 The Game....................................................... 1071 Scoring................................................. 1074 The Source Code................................................. 1076 The APPLET Tag......................................... 1076 Scrabblet.java............................................ 1077 IntroCanvas.java......................................... 1090 xxiv Java™ 2: The Complete Reference Board.java............................................... 1091 Bag.java................................................. 1109 Letter.java............................................... 1111 ServerConnection.java.................................... 1117 The Server Code................................................. 1123 Server.java.............................................. 1123 ClientConnection.java.................................... 1127 Enhancing Scrabblet.............................................. 1131 A Using Java’s Documentation Comments................. 1133 The javadoc Tags................................................. 1134 @author................................................. 1135 @deprecated............................................. 1135 {@docRoot}.............................................. 1135 @exception.............................................. 1135 {@inheritDoc}............................................ 1136 {@link}.................................................. 1136 {@linkplain}............................................. 1136 @param................................................. 1136 @return................................................. 1136 @see.................................................... 1136 @serial.................................................. 1137 @serialData.............................................. 1137 @serialField............................................. 1137 @since.................................................. 1137 @throws................................................ 1138 {@value}................................................ 1138 @version................................................ 1138 The General Form of a Documentation Comment..................... 1138 What javadoc Outputs............................................ 1138 An Example that Uses Documentation Comments.................... 1139 Index.............................................. 1141 Preface he past few years document the following fact: The Web has irrevocably recast T the face of computing and programmers unwilling to master its environment will be left behind. The preceding is a strong statement. It is also true. More and more, applications must interface to the Web. It no longer matters much what the application is, near universal Web access is dragging, pushing, and coaxing programmers to program for the online world, and Java is the language that many will use to do it. Frankly, fluency in Java is no longer an option for the professional programmer, it is a requirement. This book will help you acquire it. Aside from being the preeminent language of the Internet, Java is important for another reason: it has altered the course of computer language development. Many of the features first mainstreamed by Java are now finding their way into other languages. For example, the new C# language is strongly influenced by Java. Knowledge of Java opens the door to the latest innovations in programming. Put directly, Java is one of the world’s most important computer languages. xxv xxvi Java™ 2: The Complete Reference A Book for All Programmers To use this book does not require any previous programming experience. However, if you come from a C/C++ background, then you will be able to advance a bit more rapidly. As most readers will know, Java is similar, in form and spirit, to C/C++. Thus, knowledge of those langauges helps, but is not necessary. Even if you have never programmed before, you can learn to program in Java using this book. What’s Inside This book covers all aspects of the Java programming language. Part 1 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 1 also discusses Java’s exception-handling mechanism, multithreading subsystem, packages, and interfaces. Part 2 examines the standard Java library. As you will learn, much of Java’s power is found in its library. Topics include strings, I/O, networking, the standard utilities, the Collections Framework, applets, GUI-based controls, and imaging. Part 3 looks at some issues relating to the Java development environment, including an overview of Java Beans, Servlets, and Swing. Part 4 presents a number of high-powered Java applets that 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. What’s New in the Fifth Edition The differences between this and the previous editions of this book mostly involve those features added by Java 2, version 1.4. Of the many new features found in version 1.4, perhaps the most important are the assert keyword, the channel-based I/O subsystem, chained exceptions, and networking enhancements. This fifth edition has been fully updated to reflect those and other additions. New features are clearly noted in the text, as are features added by previous releases. This fifth edition also updates and restores the Sevlets chapter. Previously this chapter relied upon the now out-dated JSDK (Java Servlets Developers Kit) to develop and test servlets. It now uses Apache Tomcat, which is the currently recommended tool. Don’t Forget: Code on the Web Remember, the source code for all of the examples and projects in this book is available free-of-charge on the Web at www.osborne.com. Preface xxvii 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 17, 18, 23, 29, 30, 31, and 32 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 24, 25, 26, and 27. Joe has helped on several of my books and, as always, his efforts are appreciated. HERBERT SCHILDT May 25, 2002 Mahomet, Illinois xxviii Java™ 2: The Complete Reference For Further Study Java 2: 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 2: A Beginner's Guide Java 2 Programmer's Reference To learn about C++, you will find these books especially helpful: C++: The Complete Reference C++: A Beginner's Guide Teach Yourself C++ C++ From the Ground Up STL Programming From the Ground Up To learn about C#, we suggest the following Schildt books: C#: A Beginner's Guide C#: The Complete Reference If you want to learn more about the C language, the foundation of all modern programming, then the following titles will be of interest: C: The Complete Reference Teach Yourself C When you need solid answers, fast, turn to Herbert Schildt, the recognized authority on programming. Part I The Java Language This page intentionally left blank. Chapter 1 The Genesis of Java 3 4 Java™ 2: The Complete Reference hen the chronicle of computer languages is written, the following will be said: W B led to C, C evolved into C++, and C++ set the stage for 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. 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 creation of Java was driven by both elements in nearly equal measure. Java’s Lineage Java is related to C++, which is a direct descendent 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 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, 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 Chapter 1: The Genesis of Java 5 Prior to C, programmers usually had to choose between languages that optimized THE JAVA LANGUAGE 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 systems 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. 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, 6 Java™ 2: The Complete Reference 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. The Need for C++ 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

Use Quizgecko on...
Browser
Browser